如何使用随机选择的函数作为 if 语句中的条件?
How can I use a randomly chosen function as a condition in an if statement?
首先我想说的是,我在 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()
您可以通过将 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()
首先我想说的是,我在 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()
您可以通过将 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()