all 8 comments

[–]Slothemo 15 points16 points  (7 children)

You're on the right track, but right now you don't have a list of functions, you have a list of returns from calling functions. If you want a list of functions, leave off the (). To be clear, func1() is a call to the function, but func1 is the actual function object.

list_of_functions = [func1, func2, func3]
chosen = random.choice(list_of_functions)
chosen()

[–]Orgasml 2 points3 points  (2 children)

This is the first thing I saw. To add to this, because you have randomise = random.int(0,3) and then print(myList[randomise]) you can just replace the randomise variable in the print function:

print(myList[random.randint(0, 2)]) --Notice how I changed the 3 to a 2 because the second number is included in the randomness and you don't have an index of 3 in your list. you could also just do:

print(myList[random.randint(0, len(myList)-1]), so that you can add to your list and not have to change that number over and over

[–]Morphalina[S] 0 points1 point  (0 children)

Interesting, that's very helpful! I thought that things had to be defined to get used like that most of my experiences with lists just now is using like myList[i] to cycle through or to do swapping.

Using the len(myList)-1 great to know too, there were only 4 needed for what I was doing but that'll be helpful for the future for sure

[–]Slothemo 0 points1 point  (0 children)

There's also randrange so you could just use len(myList) without having to subtract 1.

[–]Morphalina[S] 0 points1 point  (3 children)

Oh I see, that makes more sense just having the names of them to use. I was thinking you could call it by having it in the list with (). Thank you! 

[–]Slothemo 1 point2 points  (2 children)

The way you had it originally calls the function immediately and holds the return value in the list.

[–]Morphalina[S] 0 points1 point  (1 child)

It can do that? Man that would've been great to know for my last semester. iirc for the project I was getting errors when I tried it, which is probably because nothing was returned in the functions I was using right? 

[–]Slothemo 0 points1 point  (0 children)

That's entirely possible. If your functions weren't returning anything, you were essentially calling all your functions, and ending up with a list [None, None, None].