pycharm hangman游戏中的问题。初学者在这里(显然)。 TypeError:类型为“ builtin_function_or_method”的对象没有len()[关闭]

问题描述 投票:-1回答:2
[在第102和103行,print(len(guess))if len(guess) != 1:'guess'突出显示,并说“ Expected'Sized',得到'()-> str相反...'”我认为这与实际错误有关... idk

完整错误:

Traceback (most recent call last): File "/Users/...", line 142, in <module> get_guess() File "/Users/...", line 102, in get_guess print(len(guess)) TypeError: object of type 'builtin_function_or_method' has no len()

代码:

# HANGMAN import random import time hangman_pics = ['+-------+\n' '| |\n' ' |\n' ' |\n' ' |\n' ' |\n' '===========]', '+-------+\n' '| |\n' 'O |\n' ' |\n' ' |\n' ' |\n' '===========]', '+-------+\n' '| |\n' 'O |\n' '| |\n' ' |\n' ' |\n' '===========]', '+-------+\n' '| |\n' 'O |\n' '|\ |\n' ' |\n' ' |\n' '===========]', '+-------+\n' '| |\n' 'O |\n' '/|\ |\n' ' |\n' ' |\n' '===========]', '+-------+\n' '| |\n' 'O |\n' '/|\ |\n' '/ |\n' ' |\n' '===========]', '+-------+\n' '| |\n' 'O |\n' '/|\ |\n' '/\ |\n' ' |\n' '===========]\n' ] words = [ "king", "fang", "long", "fast", "girl", "cats", "free", "bird", "band", "skin", "pour", "smart", "games", "perch", "clamp", "clock", "photo", "friar", "flute", "first", "candy", "mouth", ] win = False guesses = [] def get_word(): word = random.choice(words) return word def hidden_word(word, guesses): blanks = '' for i in word: blanks += f'{i} ' if i in guesses else '_ ' return blanks def get_guess(): global guess guess = input('\nYour guess: ').lower print(len(guess)) if len(guess) != 1: print(f'\n{"- - - - - " * 2}\nOnly guess one letter!\n{"- - - - - " * 2}\n') elif guess not in 'qwertyuiopasdfghjklzxcvbnm': print(f'\n{"- - - - - " * 2}\nOnly guess letters!\n{"- - - - - " * 2}\n') elif guess in guesses: print(f'\n{"- - - - - " * 2}\nYou already guessed that, idiot.\n{"- - - - - " * 2}\n') else: return guess guesses.append(guess) def correct_guess(): global lives_lost lives_lost = 0 while True: if get_guess() in guesses: print(f'\n{"- - - - - - " * 2}\nNice, {guess} is in the word.\n{"- - - - - - " * 2}\n') elif guess not in word.lower(): lives_lost += 1 print(f'\n{"- - - - " * 2}\n"{guess}" is not in the word.\n{"- - - - " * 2}\n') def display_pic(): print(hangman_pics[lives_lost]) print(hidden_word(word, guesses)) def win_or_not(): win = all(item in guesses for item in list(word)) return win while True: lives_lost = 0 word = get_word() print(f'\n\nGuess a letter. You have 6 lives. You lose a life every time you guess wrong.\n\n{"-----" * 10} ' f'\nThis word has {len(word)} letters. \n{"-----" * 10}\n') display_pic() while True: get_guess() print(guess) correct_guess() display_pic() if win_or_not() is True: print(f'\n{"- - - - - " * 4}\ngood job you won. the word is "{word}".\n{"- - - - - " * 4}') break elif lives_lost == 5 and win_or_not() is False: print(f'\n{"- - - - - " * 2}\nYOU LOSE. the word was "{word}"\n{"- - - - - " * 2}') break

python python-3.x pycharm
2个回答
1
投票
您的错误在这里:

guess = input('\nYour guess: ').lower print(len(guess))

您已将lower函数(不是调用它的结果,而是内置函数本身)分配给了变量guess。然后尝试找到它的长度。您找不到函数的len,因此会收到该错误。

将第一行更改为:

guess = input('\nYour guess: ').lower()

将其作为由[[calling lower函数产生的字符串。

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.