到目前為止,這是我的代碼(在 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()
我呼叫了該函式并運行了代碼……一切似乎都在作業,除了我真的不知道如何讓游戲回圈進行,直到玩家從 1-9 中得到正確的數字……然后結束它在我需要的時候。我嘗試搜索我所有的資源,并且非常堅持這個初學者練習編碼。任何幫助表示贊賞。
我寫的和試過的都在上面……谷歌搜索和計算器讓我更加困惑。
uj5u.com熱心網友回復:
老實說,有很多方法可以做你想做的事。但是使用您的代碼作為基礎,這是一種可能的解決方案。
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()
uj5u.com熱心網友回復:
while True:
number_game()
用這個替換腳本的最后一行!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/537557.html
下一篇:回圈洗掉所選資料框列標題中的字串
