Python 项目赢得';别再跑了
程序将不会在y输入后再次运行,请帮助,n正常工作不知道为什么,这是我的while循环吗?在这方面有很多问题,大约2小时。任何帮助都是非常感谢的。我还需要用一段时间吗Python 项目赢得';别再跑了,python,input,Python,Input,程序将不会在y输入后再次运行,请帮助,n正常工作不知道为什么,这是我的while循环吗?在这方面有很多问题,大约2小时。任何帮助都是非常感谢的。我还需要用一段时间吗 import random def main(): print("This program will play a game with you! You'll get 3 chances to guess correctly at a number chosen at random by the computer
import random
def main():
print("This program will play a game with you! You'll get 3 chances to guess correctly at a number chosen at random by the computer betwen 1 and 10")
yn = input("Would you like to play the game?(Y/N): ")
if yn.upper() != 'Y':
print("Ok have a nice day!")
the_number = random.randint(1, 10)
guess = int(input("Take a guess: "))
tries = 1
# guessing loop
while guess != the_number:
if guess > the_number:
print("Got to go lower bud")
else:
print("Got to go higher bud")
guess = int(input("Take a guess: "))
tries += 1
if tries == 3:
print ("You failed to guess in time, the number was", the_number)
break
if guess == the_number:
print("You guessed it! The number was", the_number)
print("And it only took you", tries, "tries!")
yn = input("Would you like to play the game?(Y/N): ")
if yn.upper() != 'Y':
print("Ok have a nice day!")
if __name__ == "__main__":
main()
正如@Carcigenicate所说的,您需要将其放入循环/函数或再次调用main
import random
def main():
print("This program will play a game with you! You'll get 3 chances to guess correctly at a number chosen at random by the computer betwen 1 and 10")
yn = input("Would you like to play the game?(Y/N): ")
while yn.upper() == 'Y':
the_number = random.randint(1, 10)
guess = int(input("Take a guess: "))
tries = 1
# guessing loop
while guess != the_number:
if guess > the_number:
print("Got to go lower bud")
else:
print("Got to go higher bud")
guess = int(input("Take a guess: "))
tries += 1
if tries == 3:
print ("You failed to guess in time, the number was", the_number)
break
if guess == the_number:
print("You guessed it! The number was", the_number)
print("And it only took you", tries, "tries!")
yn = input("Would you like to play the game?(Y/N): ")
print("Ok have a nice day!")
if __name__ == "__main__":
main()
它不是在一个循环中。我不希望它再次运行。您需要再次调用
main
,或者将其包装到另一个循环中。