Python 如何制作我的';秘密数字游戏';重播而不必再次执行代码?
Python初学者。我一直在尝试让我的游戏在初始游戏完成后给用户一个“再次玩”选项。如果我在6次尝试后猜不到数字,重播就可以工作,但是如果我成功地猜到数字并尝试重播,则不会发生任何事情Python 如何制作我的';秘密数字游戏';重播而不必再次执行代码?,python,Python,Python初学者。我一直在尝试让我的游戏在初始游戏完成后给用户一个“再次玩”选项。如果我在6次尝试后猜不到数字,重播就可以工作,但是如果我成功地猜到数字并尝试重播,则不会发生任何事情 import random secretNumber = random.randint(1, 20) userGuesses = 0 userInput = False while userInput == False: print("Let's play a game!") print(
import random
secretNumber = random.randint(1, 20)
userGuesses = 0
userInput = False
while userInput == False:
print("Let's play a game!")
print("I'll think of a number between 1 and 20 and you have 6 attempts to get it right.")
print("What is your first guess?")
while userGuesses <= 5:
userInput = input()
if int(userInput) > secretNumber:
print("Too High! Try again!")
userGuesses += 1
elif int(userInput) < secretNumber:
print("Too Low! Try again!")
userGuesses += 1
else:
print("Congratulations! You guessed the secret number in " + str(userGuesses + 1) + " guesses!")
print("Would you like to play again? Y or N")
playGame = input()
if playGame == "Y":
userInput = False
userGuesses = 0
else:
userInput = True
print("Goodbye!")
else:
print("You have run out of guesses! The number I was thinking of was " + str(secretNumber) + ". Better luck "
"next time!")
print("Would you like to play again? Y or N")
playGame = input()
if playGame == "Y":
userInput = False
userGuesses = 0
else:
userInput = True
print("Goodbye!")
随机导入
secretNumber=random.randint(1,20)
用户猜测=0
userInput=False
当userInput==False时:
打印(“让我们玩个游戏吧!”)
打印(“我会想出一个介于1和20之间的数字,你有6次尝试才能把它弄对。”)
打印(“你的第一个猜测是什么?”)
当用户猜测secretNumber时:
打印(“太高!重试!”)
用户猜测+=1
elif int(用户输入)
提前感谢您提供的提示。只需在“重新启动”游戏的if处添加休息时间:
import random
secretNumber = random.randint(1, 20)
userGuesses = 0
userInput = False
while userInput == False:
print("Let's play a game!")
print("I'll think of a number between 1 and 20 and you have 6 attempts to get it right.")
print("What is your first guess?")
while userGuesses <= 5:
userInput = input()
if int(userInput) > secretNumber:
print("Too High! Try again!")
userGuesses += 1
elif int(userInput) < secretNumber:
print("Too Low! Try again!")
userGuesses += 1
else:
print("Congratulations! You guessed the secret number in " + str(userGuesses + 1) + " guesses!")
print("Would you like to play again? Y or N")
playGame = input()
if playGame == "Y":
userInput = False
userGuesses = 0
#Break here to exit while loop
break
else:
userInput = True
print("Goodbye!")
else:
print("You have run out of guesses! The number I was thinking of was " + str(secretNumber) + ". Better luck "
"next time!")
print("Would you like to play again? Y or N")
playGame = input()
if playGame == "Y":
userInput = False
userGuesses = 0
else:
userInput = True
print("Goodbye!")
随机导入
secretNumber=random.randint(1,20)
用户猜测=0
userInput=False
当userInput==False时:
打印(“让我们玩个游戏吧!”)
打印(“我会想出一个介于1和20之间的数字,你有6次尝试才能把它弄对。”)
打印(“你的第一个猜测是什么?”)
当用户猜测secretNumber时:
打印(“太高!重试!”)
用户猜测+=1
elif int(用户输入)
希望这对你有帮助 您还可以在单独的函数中定义游戏。我会这样做:
import random
def play_game():
secretNumber = random.randint(1, 20)
userGuesses = 0
userInput = False
print("Let's play a game!")
print("I'll think of a number between 1 and 20 and you have 6 attempts to get it right.")
print("What is your first guess?")
while userGuesses <= 5:
userInput = input()
if int(userInput) > secretNumber:
print("Too High! Try again!")
userGuesses += 1
elif int(userInput) < secretNumber:
print("Too Low! Try again!")
userGuesses += 1
else:
print("Congratulations! You guessed the secret number in " + str(userGuesses + 1) + " guesses!")
print("You have run out of guesses! The number I was thinking of was " + str(secretNumber) + ". Better luck "
"next time!")
if __name__ == '__main__':
playGame = 'y'
while playGame == 'y':
play_game()
playGame = input('Would you like to play again? [y/n] ').lower()
print("Goodbye!")
随机导入
def play_game():
secretNumber=random.randint(1,20)
用户猜测=0
userInput=False
打印(“让我们玩个游戏吧!”)
打印(“我会想出一个介于1和20之间的数字,你有6次尝试才能把它弄对。”)
打印(“你的第一个猜测是什么?”)
当用户猜测secretNumber时:
打印(“太高!重试!”)
用户猜测+=1
elif int(用户输入)
只需在循环时从内部中删除输入问题,一旦用户猜到它,就可以中断循环