我已经为此工作了一段时间。我已经能够让这部分工作,但从来没有完成。最终目标是将用户循环回另一个游戏,如果他们愿意的话。我认为问题出在我的 break 语句上,但我不确定如何绕过它。我已经包含了我所有的代码,以便可以发现任何错误。抱歉,如果已经回答了这个问题,我找不到存在此类问题的页面。def game(): import random from random import randint n = randint(1, 10) print('Enter a seed vlaue: ') the_seed_value = input(' ') random.seed(the_seed_value) guessesTaken = 0 print("what is your name?") myName = input("") guess = int(input("Enter an integer from 1 to 99: ")) while n != "guess": if guess < n: print ("guess is low") guessesTaken = guessesTaken + 1 guess = int(input("Enter an integer from 1 to 99: ")) elif guess > n: print ("guess is high") guessesTaken = guessesTaken + 1 guess = int(input("Enter an integer from 1 to 99: ")) else: print ("Congradulations " + myName + " you guessed it in " + str(guessesTaken) + " guesses!") break print('Want to play agian? y/n') answer = input(" ") if answer == "n": print ("Ok, See you next time!") elif answer == "y": print("Starting new game!") game()def main(): game()if __name__ == "__main__": main()
1 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
一方面,@kerwei 正确指出您的while线路有问题,需要从 更改while n != "guess":为while n != guess:。
对于两个,当玩家正确猜测时,你的 while 循环就会得到满足,绕过这Congrats条线。
由于游戏目前的结构是保持循环直到玩家猜对了,一个简单的解决方法是else:从循环中删除该行并在之后放置胜利语句。那是,
def game()
...
while n != guess:
if guess < n:
...
elif guess > n:
...
print('Congrats!')
print('play again?')
...
添加回答
举报
0/150
提交
取消