[Python] How to call functions in a random order

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have a large number of functions in a sequence (named Q1, Q2, Q3...Q1020), and I want to call 8 functions randomly from a user-defined range: the smallest range would be 8, and the largest range would be over 1000. Newbie.

    To call a random function, I tried generating a random number, then constructing the name of the function, then calling it. I get this error message:


    TypeError: 'str' object is not callable
    ​

    def Q6 ():
    x = 3
    return x

    a = "Q"
    b = "6"
    my_function = a + b
    my_function()


    And this:

    a = "Q"
    b = "6"
    my_function = a + b
    call_function = print (my_function)
    call_function ()


    I have done this successfully in other languages using printf, but I don't know how to do it in Python.

    # Make the name of the function.
    callRandomFunction=$(printf "Q%d\n" $randomQuestion)

    # Call the function.
    $callRandomFunction

    Continue reading...

Compartilhe esta Página