【问题标题】:Picking random sub-routines from a list从列表中选择随机子例程
【发布时间】:2014-11-04 15:04:25
【问题描述】:

这适用于 Python 3.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))

【问题讨论】:

    标签: python list random python-3.3 subroutine


    【解决方案1】:

    删除括号,否则您将在创建列表时调用所有函数。

    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))
    

    【讨论】:

    • 我试过 my_list=[func_addition , func_subtraction , func_multiplication] 没有括号。但是,当我运行它时,它会说:你叫什么名字? Thifyan 你好,欢迎来到算术测验
    • @ThifyanRavinehru 您正在打印您选择的功能。您需要使用 random.choice(my_list)() 来运行它
    • 能否请您立即发布整个代码。我很困惑。谢谢
    • @ThifyanRavinehru,用我刚刚添加的行替换你的最后四行
    【解决方案2】:
    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] #without parentheses
    
    name=input("What is your name ? ")
    print("Hello "+str(name)+" and welcome to The Arithmetic Quiz")
    random.choice(my_list)()
    

    【讨论】:

    • 两次random.choice?
    • @Matthias 谢谢,忘记删除打印声明。
    • 我试过 my_list=[func_addition , func_subtraction , func_multiplication] 没有括号。但是,当我运行它时,它会说:你叫什么名字? Thifyan 你好,欢迎来到算术测验
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 2012-04-30
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多