[英]beginner python on looping back to the start of my simple number guessing game
到目前为止,这是我的代码(在 PyCharm 中),我正在编写一个非常简单的数字猜谜游戏,其中包含 1-9 的整数。 我仍在努力掌握思维和流程以及循环,但我遇到了障碍:
import random
Player_Name = input("What is your name?\n")
print(f"Hello {Player_Name}!\n")
random_num = random.randint(1, 10)
guess = int(input("What is the number you want to pick? Guess one, 1-9\n"))
def number_game():
if guess == random_num:
print(f"You guessed right, the number is confirmed to be {random_num}.")
else:
print(f"You guessed the wrong number. Try again.\n")
number_game()
我打电话给 function 并运行了代码……一切似乎都正常,除了我真的不知道如何让游戏循环进行,直到玩家从 1-9 中得到正确的数字……然后结束它在我需要的时候。 我尝试搜索我所有的资源,并且非常坚持这个初学者练习编码。 任何帮助表示赞赏。
我写的和试过的都在上面……谷歌搜索和计算器让我更加困惑。
老实说,有很多方法可以做你想做的事。 但是使用您的代码作为基础,这是一种可能的解决方案。
import random
Player_Name = input("What is your name?\n")
print(f"Hello {Player_Name}!\n")
random_num = random.randint(1, 10)
def number_game():
guess = int(input("What is the number you want to pick? Guess one, 1-9\n"))
if guess == random_num:
print(f"You guessed right, the number is confirmed to be {random_num}.")
return True
else:
print(f"You guessed the wrong number. Try again.\n")
return False
while True:
guessed_right = number_game()
if guessed_right:
quit()
else:
number_game()
while True:
number_game()
用这个替换脚本的最后一行!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.