0

I've been working on a game, and I'm trying to go to random functions. Here's my code:

import random


def choice1():
  print("This is in choice 1 right now.")


def choice2():
  print("This is in choice 2 right now.")


def choice3():
  print("This is in choice 3 right now.")  


room_list = [choice1, choice2, choice3]
new_room = random.choice(room_list)

Just calling the variable doesn't work. Any help would be appreciated. Thanks!

chepner
  • 497,756
  • 71
  • 530
  • 681
Wicked505
  • 3
  • 1

1 Answers1

6

random.choice(room_list) returns a function, you still have to call it:

import random


def choice1(): 
    print("This is in choice 1 right now.")

def choice2(): 
    print("This is in choice 2 right now.")

def choice3(): 
    print("This is in choice 3 right now.")

room_list = [choice1, choice2, choice3]
new_room = random.choice(room_list)()  # note the () to call the function

Sample output:

This is in choice 2 right now.
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • Just to note, a temporary variable can make this easier to read; `chooser = random.choice(room_list); new_room = chooser()`. The lone `()` at the end is easy to lose sight of in a chain. – chepner Dec 02 '19 at 14:42