我的Hangman代码在第40行,第40行有错误
print ('_'+ end=' ')
我的代码:
import time
import random
print('Lets play hangman')
print("""
_______
| |
| O
| |
| \- -/
| |
| | |
| | |
____|____
""")
random_words = ['string', 'loop', 'python', 'print', 'run' , 'graphics window', 'variable', 'iteration', 'modules', 'module', 'input', 'logic', 'output ']
time.sleep(2)
print ("What level would you like to play at? Too bad, you get random! Better type it in tho...")
level_of_difficulty = "Random"
time.sleep(2)
print ("The program is now generating your word...")
if level_of_difficulty == 'Random':
generated_word = random.choice(random_words)
guessed = ''
lives = 7
while lives > 0:
missed = 0
print()
for letter in generated_word:
if letter in guessed:
print("Congrats ya dumb animal, you got one")
else:
print ('_'+ end=' ')
missed = missed + 1
if missed == 0:
print ('\n\nYou win!')
quit()
break
guess=input("Well what are you waiting for? guess a letter!:")
guessed = guessed + guess
if guess not in generated_word:
print ('\n People like you are the reason why the human race will fail')
print ('Fool')
lives = lives - 1
missed = missed + 1
print('your new lives value is ' + str(lives))
if lives < 7:
print(''' _______
| | ''')
if lives < 6:
print(' | O ')
if lives < 5:
print(' | | ')
if lives < 4:
print(' | \- -/ ')
if lives < 3:
print(' | | ')
if lives < 2:
print(' | | | ')
if lives < 1:
print(' | | | ')
if lives == 0:
print('___|___ ')
print('Game Over Boi')
print('The secret word was ' + str(generated_word) + "fool")
quit()
答案 0 :(得分:0)
所以这是部分属于我的代码
这似乎是问题的根源。除了类型之外,主要问题是在使用Python 2时使用Python 3功能。
print ('_'+ end=' ')
除了输入错误外,+
必须是,
。在Python 2中,end
关键字不用于打印,仅在python 3中使用。相反,您可以在Python 2中执行以下操作:
print '_',
下一个问题将与以下行:
guess=input("Well what are you waiting for? guess a letter!:")
这会给您一个错误,因为在Python 2中,它会尝试评估您作为表达式而不是字符串输入的内容。相反,您需要这样做:
guess=raw_input("Well what are you waiting for? guess a letter!:")
使用这两个修复程序,您将获得一个working game。尽管您可能希望添加某种方式来查看用户正确输入的字母。