我必须为我的电脑课程制作这个游戏,但是我无法弄清如何打破这个循环。你看,我必须和“电脑”对战,通过掷出更大的数字并查看谁得分更高来进行比赛。但是我不知道如何从自己的回合中“退出”,并转入电脑的回合。我需要在“Q”(退出)信号的指示下开始电脑的回合,但我不知道如何做到。
ans=(R)
while True:
print('Your score is so far '+str(myScore)+'.')
print("Would you like to roll or quit?")
ans=input("Roll...")
if ans=='R':
R=random.randint(1, 8)
print("You rolled a "+str(R)+".")
myScore=R+myScore
if ans=='Q':
print("Now I'll see if I can break your score...")
break
break
的方式是可以的,但你必须准确地输入Q
。例如,小写字母q
是不行的。第一行代码应该是ans=('R')
吗?但无论如何你都不需要它。 - John La Rooy