[英]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.