-1
first_operand = random.randint(0, 9)

    numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    first_operand = numbers[first_operand]

    #funcynum is the name of another module. it contains the functions print_zero, print_one, etc. 

    first_number = 'funcynum.print_' + first_operand
    return first_number

I have 10 different functions (not shown above), all similarly named (i.e. print_zero, print_one, print_two, print_three, etc.) The point of my program is to generate a random number and call a function according to its number. For instance, if the program randomly generates the number 3, it would call the function print_three. So far, I have a list that contains the written words for numbers 0 - 9. The program then retrieves the word according to the generated number. However, the program returns a string of the function but does not actually call it. How would I change my code so that it would call the function instead?

Allen Lee
  • 1
  • 1

1 Answers1

1

I believe the best way to do this would be to replace your numbers list with a list of functions

first_operand = random.randint(0, 9)

#instantiate the list with functions
funcs = [funcynum.print_zero, funcynum.print_one, ... funcynum.print_nine] 

return funcs[first_operand]()
ngood97
  • 513
  • 5
  • 16