import random
guess = ''
while guess not in ('heads','tails'):
print('Guess the coin toss! Enter heads or tails:')
guess = input()
toss = random.randint(0, 1) # 0 is tails, 1 is heads
toss=['tails','heads'][toss]
if toss == guess:
print('You got it!')
else:
print('Nope! Guess again!')
guess = input()
if toss == guess:
print('You got it!')
else:
print('Nope. You are really bad at this game.')
图片是原来的代码,下面的是修改后的代码,原来的代码主要有2错误。1是toss是数字,而输入是字符串,永远都无法相等。
2是第二次输入时变量错写为guesss,正确应该为guess