Python 从列表中随机选取子例程

Python 从列表中随机选取子例程,python,list,random,python-3.3,subroutine,Python,List,Random,Python 3.3,Subroutine,这是针对Python3.3的。当我运行这个程序时,它总是运行子程序“func_addition” 我想让它从列表中随机选取一个子例程。所以,它会问一个随机的算术问题 import random def func_addition(): a = random.randint(1,25) b = random.randint(1,25) c=a+b answer=int(input("What is "+str(a)+" + "+str(b)+" ? "))

这是针对Python3.3的。当我运行这个程序时,它总是运行子程序“func_addition”

我想让它从列表中随机选取一个子例程。所以,它会问一个随机的算术问题

import random
def func_addition():
    a = random.randint(1,25)
    b = random.randint(1,25)
    c=a+b
    answer=int(input("What is "+str(a)+" + "+str(b)+" ? "))   

def func_subtraction():
    d = random.randint(10,25)
    e = random.randint(1,10)
    f=d-e
    answer=int(input("What is "+str(d)+" - "+str(e)+" ? "))

def func_multiplication():
    g = random.randint(1,10)
    h = random.randint(1,10)
    i=g*h
    answer=int(input("What is "+str(g)+" X "+str(h)+" ? "))

my_list=[func_addition() , func_subtraction() , func_multiplication()]

name=input("What is your name ? ")
print("Hello "+str(name)+" and welcome to The Arithmetic Quiz")
print(random.choice(my_list))

删除paren,否则在创建列表时将调用所有函数

my_list = [func_addition , func_subtraction , func_multiplication]

name = input("What is your name ? ")
print("Hello {} and welcome to The Arithmetic Quiz".format(name))
chc = random.choice(my_list) # pick random function
chc() # call function
如果您不想使用变量,我将执行以下操作来验证答案:

def func_addition():
    a = random.randint(1,25)
    b = random.randint(1,25)
    c = a + b
    answer = int(input("What is {} + {} ? ".format(a,b)))
    if answer == c:
        print("Well done, that is correct")
    else:
        print(" Sorry, that is incorrect, the correct answer is {}".format(c))

两次
random.choice
?@Matthias谢谢,忘了删除打印语句。我尝试了不带括号的[func\u加法、func\u减法、func\u乘法]列表。但是,当我运行它时,它会说:你叫什么名字?Thifyan你好Thifyan欢迎参加算术测验我试过我的列表=[func_加法,func_减法,func_乘法],没有括号。但是,当我运行它时,它会说:你叫什么名字?Thifyan您好Thifyan,欢迎来到算术测验@ThifyanRavinehru,您正在打印您选择的函数。你需要用random.choice(我的列表)来运行它。你能马上发布整个代码吗。我很困惑。感谢you@ThifyanRavinehru,将最后四行替换为我刚才添加的行
def func_addition():
    a = random.randint(1,25)
    b = random.randint(1,25)
    c = a + b
    answer = int(input("What is {} + {} ? ".format(a,b)))
    if answer == c:
        print("Well done, that is correct")
    else:
        print(" Sorry, that is incorrect, the correct answer is {}".format(c))