我已经为此工作了一段时间。我已经能够使其中的一部分起作用,但没有全部起作用。最终目标是在用户选择的情况下将其循环回另一个游戏。我认为问题出在我的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()
答案 0 :(得分:0)
例如,@ kerwei正确地指出您的while
行存在问题,需要将其从while n != "guess":
更改为while n != guess:
。
对于两个玩家,当玩家正确猜测时,绕过Congrats
行即可满足您的while循环。
由于游戏目前的结构是一直处于循环状态,直到玩家正确猜出为止,一个简单的解决方法是从循环中删除else:
行,然后放置胜利声明。也就是说,
def game()
...
while n != guess:
if guess < n:
...
elif guess > n:
...
print('Congrats!')
print('play again?')
...