我想首先說我對 Python 處于非常初級的水平,可能還沒有以最有效的方式進行編碼。
我正在嘗試構建一個程式,為用戶提供一個簡單的數學測驗。它隨機選擇一個問題(加法、減法、除法或乘法)并要求用戶輸入來回答問題。
如果用戶做對了,我要祝賀他們。如果他們弄錯了,我想給他們正確的答案。我為我想提供給用戶的每種不同型別的問題創建了函式,并將它們添加到串列中。然后我隨機選擇了一個要呼叫的函式并提供給用戶。
從這里開始,我想我只需將變數 ( prob)分配給隨機選擇的函式,并將其用作一系列if else陳述句中的條件。這不起作用。相反,它會跳過我希望它發回給他們的訊息,然后轉到我的下一個input命令。我猜這是因為我無法真正為隨機選擇函式分配一個變數。
我還要如何撰寫代碼才能讓它做我想要它做的事情?
import random
def problem():
global add
def add():
num1 = random.randint(0, 999)
num2 = random.randint(0, 999)
global addition
addition = num1 num2
op1 = f' {num1}\n {num2}'
print(op1)
global sub
def sub():
num1 = random.randint(0, 999)
num2 = random.randint(0, 999)
global subtract
subtract = num1 - num2
op2 = f' {num1}\n-{num2}'
if subtract < 0:
sub()
elif subtract > 0:
print(op2)
global mult
def mult():
num1 = random.randint(1, 12)
num2 = random.randint(1, 12)
global multiply
multiply = num1 * num2
op3 = f'{num1} * {num2}'
print(op3)
global div
def div():
num1 = random.randint(0, 150)
num2 = random.randint(0, 150)
global divide
divide = num1 / num2
op4 = f'{num1} / {num2}'
if num1 % num2 == 0:
print(op4)
elif num1 % num2 != 0 or num2 == 0:
div()
global numlist
numlist = [add, sub, mult, div]
prob = random.choice(numlist)()
answer = input()
if prob == numlist[0] and answer == f'{add.addition}':
print(f'Congrats! You got it right.')
elif prob == numlist[0] and answer != f'{add.addition}':
print(f'Incorrect. The answer is {add.addition}.')
elif prob == numlist[1] and answer == f'{sub.subtract}':
print(f'Congrats! You got it right.')
elif prob == numlist[1] and answer != f'{sub.subtract}':
print(f'Incorrect. The answer is {sub.subtract}.')
elif prob == numlist[2] and answer == f'{mult.multiply}':
print(f'Congrats! You got it right.')
elif prob == numlist[2] and answer != f'{mult.multiply}':
print(f'Incorrect. The answer is {mult.multiply}.')
elif prob == numlist[3] and answer == f'{div.divide}':
print(f'Congrats! You got it right.')
elif prob == numlist[3] and answer != f'{div.divide}':
print(f'Incorrect. The answer is {div.divide}.')
done = input('Type "done" if you want to be done. Press Enter if you want another problem.\n')
while done == '':
problem()
if done == 'done' or done == 'Done':
break
problem()
uj5u.com熱心網友回復:
您可以通過將 prob 替換為包含randrange函式生成的亂數的變數來實作。此外,您的代碼中有幾個錯誤add.addition是沒有意義的,因為函式不能具有任何屬性。我把它改成只是addition為了修復所有的編譯器錯誤,所以最終的代碼是這樣的:
import random
def problem():
global add
def add():
num1 = random.randint(0, 999)
num2 = random.randint(0, 999)
global addition
addition = num1 num2
op1 = f' {num1}\n {num2}'
print(op1)
global sub
def sub():
num1 = random.randint(0, 999)
num2 = random.randint(0, 999)
global subtract
subtract = num1 - num2
op2 = f' {num1}\n-{num2}'
if subtract < 0:
sub()
elif subtract > 0:
print(op2)
global mult
def mult():
num1 = random.randint(1, 12)
num2 = random.randint(1, 12)
global multiply
multiply = num1 * num2
op3 = f'{num1} * {num2}'
print(op3)
global div
def div():
num1 = random.randint(0, 150)
num2 = random.randint(0, 150)
global divide
divide = num1 / num2
op4 = f'{num1} / {num2}'
if num1 % num2 == 0:
print(op4)
elif num1 % num2 != 0 or num2 == 0:
div()
global numlist
numlist = [add, sub, mult, div]
num = random.randrange(4)
prob = numlist[num]()
answer = input()
if num == 0 and answer == f'{addition}':
print(f'Congrats! You got it right.')
elif num == 0 and answer != f'{addition}':
print(f'Incorrect. The answer is {addition}.')
elif num == 1 and answer == f'{subtract}':
print(f'Congrats! You got it right.')
elif num == 1 and answer != f'{subtract}':
print(f'Incorrect. The answer is {subtract}.')
elif num == 2 and answer == f'{multiply}':
print(f'Congrats! You got it right.')
elif num == 2 and answer != f'{multiply}':
print(f'Incorrect. The answer is {multiply}.')
elif num == 3 and answer == f'{divide}':
print(f'Congrats! You got it right.')
elif num == 3 and answer != f'{divide}':
print(f'Incorrect. The answer is {divide}.')
done = input('Type "done" if you want to be done. Press Enter if you want another problem.\n')
while done == '':
problem()
if done == 'done' or done == 'Done':
break
problem()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/348437.html
