我正在嘗試撰寫一個程式如下:
- Python 生成隨機乘法(因子是從 1 到 9 的亂數)并要求用戶提供結果
- 如果用戶輸入“q”,則可以退出程式(統計資料將被計算和列印)
- 如果用戶提供了錯誤的答案,他們應該能夠重試,直到他們給出正確的答案
- 如果用戶以字串(例如“dog”)作為回應,Python 應該回傳一個錯誤并要求輸入一個整數
似乎我能夠執行 1) 和 2)。但是我不能做 3) 和 4)。當用戶給出錯誤答案時,會生成一個新的隨機乘法。
請有人幫助我嗎?
謝謝!
import random
counter_attempt = -1
counter_win = 0
counter_loss = 0
while True:
counter_attempt = 1
num_1 = random.randint(1, 9)
num_2 = random.randint(1, 9)
result = str(num_1 * num_2)
guess = input(f"How much is {num_1} * {num_2}?: ")
if guess == "q":
print(f"Thank you for playing, you guessed {counter_win} times, you gave the wrong answer {counter_loss} times, on a total of {counter_attempt} guesses!!!")
break
elif guess == result:
print("Congratulations, you got it!")
counter_win = 1
elif guess != result:
print("Wrong! Please try again...")
counter_loss = 1
uj5u.com熱心網友回復:
嗨,我的想法是將求解部分放在一個函式中:
import random
counter_attempt = -1
counter_win = 0
counter_loss = 0
def ask(num1, num2, attempt, loss, win):
result = str(num1 * num2)
guess = input(f"How much is {num1} * {num2}?: ")
if guess == "q":
print(f"Thank you for playing, you guessed {win} times, you gave the wrong answer {loss} times, on a total of {attempt} guesses!!!")
return attempt, loss, win, True
try:
int(guess)
except TypeError:
print("Please insert int.")
guess = input(f"How much is {num1} * {num2}?: ")
if guess == result:
print("Congratulations, you got it!")
win = 1
return attempt, loss, win, False
elif guess != result:
print("Wrong! Please try again...")
loss = 1
attempt = 1
return ask(num1, num2, attempt, loss, win)
while True:
num_1 = random.randint(1, 9)
num_2 = random.randint(1, 9)
counter_attempt, counter_loss, counter_win, escape = ask(num_1, num_2, counter_attempt, counter_loss, counter_win)
if escape:
break
這是你要求的嗎?
uj5u.com熱心網友回復:
請注意,while 回圈中的所有內容都會在每次迭代中發生。具體來說,包括:
num_1 = random.randint(1, 9)
num_2 = random.randint(1, 9)
因此,您確實每次都生成新的亂數(然后使用 向用戶宣布它們的生成guess = input(f"How much is {num_1} * {num_2}?: "),這也在回圈中)。
假設您只打算生成一對亂數,并且只列印“多少是...?” 訊息一次,您應該避免將它們放置在回圈中(input當然,除非實際呼叫:您確實希望重復該操作,大概,否則您只會從用戶那里獲得一次輸入)。
我強烈建議“在頭腦中運行代碼”:只需用手和筆和紙逐行寫下變數的值,并確保您了解每個變數和每條指令后發生的情況在任何特定時刻;你會親眼看到為什么會發生這種情況,并很快就會感覺到。
完成后,您可以使用附加的除錯器運行它,以查看它是否如您所想。(我個人認為,正如我在前幾次中所描述的那樣,“手動”進行操作是有好處的,只是為了確保您確實遵循邏輯。)
uj5u.com熱心網友回復:
每次用戶出錯時,您都會生成一個新的亂數,因為
num_1 = random.randint(1, 9)
num_2 = random.randint(1, 9)
result = str(num_1 * num_2)
處于 while True 回圈中。
這是固定代碼:
import random
counter_attempt = 0
counter_win = 0
counter_loss = 0
while True:
num_1 = random.randint(1, 9)
num_2 = random.randint(1, 9)
result = str(num_1 * num_2)
while True:
guess = input(f"How much is {num_1} * {num_2}?: ")
if guess == "q":
print(f"Thank you for playing, you guessed {counter_win} times, you gave the wrong answer {counter_loss} times, on a total of {counter_attempt} guesses!!!")
input()
quit()
elif guess == result:
print("Congratulations, you got it!")
counter_win = 1
break
elif guess != result:
print("Wrong! Please try again...")
counter_loss = 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/394350.html
標籤:Python
上一篇:嘗試將多個影像黑白,Python
