Python中的Hangman:试图在空白处显示猜测的字母时出现类型错误



以下代码适用于Hangman游戏。它得到一个赋值display = list("_" * len(chosen_word))的类型错误,表示它是一个Nonetype。这是因为chosen_word没有赋值(这本身就是另一个错误(吗?。如何纠正?任何其他改进该准则的建议也受到欢迎。

import random
Dictionary = {"fruits": "watermelon", "buildings": "apartment", "mammal": "horse", "occupation": "fireman"}

def choose_word():
hint, chosen_word = random.choice(list(Dictionary.items()))
print("Hint: " + hint)
blank = []
for letter in chosen_word:
blank.append("_")
print("".join(blank))
return chosen_word

def draw_hangman(attempt):
stages = [  # final state: head, torso, both arms, and both legs
"""
--------
|      |
|      O
|     \|/
|      |
|     / \
-
""",
# head, torso, both arms, and one leg
"""
--------
|      |
|      O
|     \|/
|      |
|     / 
-
""",
# head, torso, and both arms
"""
--------
|      |
|      O
|     \|/
|      |
|      
-
""",
# head, torso, and one arm
"""
--------
|      |
|      O
|     \|
|      |
|     
-
""",
# head and torso
"""
--------
|      |
|      O
|      |
|      |
|     
-
""",
# head
"""
--------
|      |
|      O
|    
|      
|     
-
""",
# initial empty state
"""
--------
|      |
|      
|    
|      
|     
-
"""
]
return stages[attempt]

def play_hangman(chosen_word):
attempt = 6
guessed = False
guessed_letters = []
display = list("_" * len(chosen_word))
while attempt > 0 and not guessed:
print(draw_hangman(attempt))
player_guess = input("nPlease guess a letter between A-Zn")
letter = 0
if len(player_guess) == 1 and player_guess.isalpha() and player_guess in chosen_word:
while chosen_word.find(player_guess, letter) != -1:
letter = chosen_word.find(player_guess, letter)
display[letter] = player_guess
letter += 1
guessed_letters.append(player_guess)
print("".join(display))
if display[letter] == chosen_word:
guessed = True
print("Congratulation, you won!")
elif player_guess in guessed_letters:
print("You have already guessed this letter")
else:
print("Your guess is not valid")
attempt -= 1
print(draw_hangman(attempt))
else:
print("You ran out of attempts")

play_hangman(choose_word())

错误1(已解决(:

Traceback (most recent call last):
File "/Users/owly/PycharmProjects/pythonProject1/main12.py", line 119, in <module>
play_hangman(choose_word())
File "/Users/owly/PycharmProjects/pythonProject1/main12.py", line 94, in play_hangman
display = list("_" * len(chosen_word))
TypeError: object of type 'NoneType' has no len()

错误2:经过几次猜测,索引超出范围

Traceback (most recent call last):
File "/Users/owly/PycharmProjects/pythonProject1/main12.py", line 120, in <module>
play_hangman(choose_word())
File "/Users/owly/PycharmProjects/pythonProject1/main12.py", line 107, in play_hangman
if display[letter] == chosen_word:
IndexError: list index out of range
w__er_e__n
Process finished with exit code 1

以下代码运行良好,

str1 = ''.join(display)
if str1 == chosen_word:
guessed = True
print("Congratulation, you won!")
attempt=-1

我刚刚添加了上面的代码和if-else条件中的一些小更改以及一些打印语句。如果需要,请删除多余的打印语句!

完整代码-

import random
Dictionary = {"fruits": "watermelon", "buildings": "apartment", "mammal": "horse", "occupation": "fireman"}

def choose_word():
hint, chosen_word = random.choice(list(Dictionary.items()))
print("Hint: " + hint)
blank = []
for letter in chosen_word:
blank.append("_")
print("".join(blank))
return chosen_word

def draw_hangman(attempt):
stages = [  # final state: head, torso, both arms, and both legs
"""
--------
|      |
|      O
|     \|/
|      |
|     / \
-
""",
# head, torso, both arms, and one leg
"""
--------
|      |
|      O
|     \|/
|      |
|     / 
-
""",
# head, torso, and both arms
"""
--------
|      |
|      O
|     \|/
|      |
|      
-
""",
# head, torso, and one arm
"""
--------
|      |
|      O
|     \|
|      |
|     
-
""",
# head and torso
"""
--------
|      |
|      O
|      |
|      |
|     
-
""",
# head
"""
--------
|      |
|      O
|    
|      
|     
-
""",
# initial empty state
"""
--------
|      |
|      
|    
|      
|     
-
"""
]
return stages[attempt]

def play_hangman(chosen_word):
attempt = 6
guessed = False
guessed_letters = []
display = list("_" * len(chosen_word))

while attempt > 0:
if not guessed:
print(draw_hangman(attempt))
player_guess = input("nPlease guess a letter between A-Zn")
letter = 0
if len(player_guess) == 1 and player_guess.isalpha() and player_guess in chosen_word and player_guess not in guessed_letters:
while chosen_word.find(player_guess, letter) != -1:
print("running while:"+str(letter))
letter = chosen_word.find(player_guess, letter)
display[letter] = player_guess
letter += 1
guessed_letters.append(player_guess)

print("".join(display))
print(display)
str1 = ''.join(display)
if str1 == chosen_word:
guessed = True
print("Congratulation, you won!")
attempt=-1
elif player_guess in guessed_letters:
print("You have already guessed this letter")
else:
print("Your guess is not valid")
attempt -= 1
print(draw_hangman(attempt))
else:
print("You ran out of attempts")

play_hangman(choose_word())

如果有什么困难,请告诉我!!

相关内容

最新更新