Python 即使我给出了错误的答案,我的程序也会给我分数
我的程序一直让我永远继续下去,即使我做得不对,除非我输入了一个大于3的数字 如果您试用我的代码,您就会明白我的意思:Python 即使我给出了错误的答案,我的程序也会给我分数,python,Python,我的程序一直让我永远继续下去,即使我做得不对,除非我输入了一个大于3的数字 如果您试用我的代码,您就会明白我的意思: #Ghost Game while True: print ('Welcome To...') from random import randint print ('Ghost Game') feeling_brave = True score = 0 while feeling_brave: ghost_d
#Ghost Game
while True:
print ('Welcome To...')
from random import randint
print ('Ghost Game')
feeling_brave = True
score = 0
while feeling_brave:
ghost_door = randint(1,3)
print('Three doors ahead...')
print('A ghost behind one.')
print('which door do you open?')
door = input('1,2 or 3')
door_num = int(door)
if door_num == ghost_door:
print('GHOST!')
feeling_brave = False
if door_num > 3:
print('GHOST!')
feeling_brave = False
else:
print('No Ghost')
print('You enter the next room...')
score = score + 1
print('Run away!')
print('GAME OVER YOU SCORED', score)
您需要在此处使用
elif
而不是第二个if
:
if door_num == ghost_door:
print('GHOST!')
feeling_brave = False
elif door_num > 3:
print('GHOST!')
feeling_brave = False
else:
print('No Ghost')
print('You enter the next room...')
score = score + 1
否则else
独立于第一个if
测试执行,前提是door_num
不大于3
如果
是单个语句,则附加可选的elif
和else
块;Python将始终最多执行一个附加块(和else
否则)
通过使用两个独立的
if
语句,第二个if
请求Python执行两个块中的一个;当door\u num
大于3时,打印GHOST
,或者增加分数,如果door\u num==GHOST\u door测试结果为3,则不管第一个是什么,都要这样做。您可能的意思是elif door\u num>3:
。。。