If Statement Based on Outcome of random.choice()

a = """foo"""
b = """foo2"""
c = """foo3"""

mylist = [a, b, c]

def example():
print (random.choice(mylist))
    if "foo2" in mylist:
        quit(0)
    else:
        example2()

I’m using triple quotes for the variables because those strings are quite long in my project.

Anyway, I easily found how to pull a random item from a list, but couldn’t find ways to then utilize the randomized outcome. The code I’m using doesn’t work. The script always ignores the if "foo2" in mylist: and moves right onto example2(). At least I think that’s what it’s doing.

I also tried:

def example():
print (random.choice(mylist))
    if mylist == a:
        quit(0)
    else:
        example2()

Any help is appreciated.

What are you trying to do? Is that all your code or just fragment of it?

Things that don’t seem right to start with and or course your code does not run as it is:

  • You are not calling the example function
  • If you were, I think you want to pass in mylist as a parameter but function definition does not seem to be requiring pameters
  • First ‘print’ statement within the function is not indented as expected

Just with that your code does not run. I am willing to take a look and give it a try if you correct it and also provide what you really have (not sure if ‘example2’ is real or not since is a function not included in your snippet).

First off, thank you for pointing out my mistakes!

Boy, did I botch the code I posted. My apologies. That’s a fragment of my code. I’m very new to this. It should have looked like this:

a = """foo"""
b = """foo2"""
c = """foo3"""

mylist = [a, b, c]

def example():
    print (random.choice(mylist))
        if "foo" in mylist:
            quit(0)
        elif "foo2" in mylist:
            print "Good job."        
        else:
            example2()

example()

Essentially, I’m trying to make a text adventure game to practice my chops. I’ve made each “room” in the game a function. The user wakes up in an apartment complex and has to escape. When a user chooses to go in certain rooms, I want the outcome to be random. Then, I want to either quit the game, print a string, or go to a different function, based on which variable comes out of random.choice().

example2() isn’t shown in the code, but that function will be a new room, which allows for more choices, based on user input.

This code occurs after the user has moved through at least 4 different rooms. I didn’t want to post everything that came before it because it seemed unnecessary. I apologize if that just confuses things. Thanks again!

With very few edits to your code, this runs for me:

A). Resulting in the else statement:

import random

    a = 1 
    b = 2 
    c = 3 

    mylist = [a, b, c]

    def example():
        print (random.choice(mylist))
        if "foo" in mylist:
            quit(0)
        elif "foo2" in mylist:
            print("Good job.")
        else:
            print('example 2')
            # example2()

    example()

B). Resulting in the if or elif statement:

import random

a = """foo"""
b = """foo2"""
c = """foo3"""

mylist = [a, b, c]

def example():
    print (random.choice(mylist))
    if "foo" in mylist:
        quit(0)
    elif "foo2" in mylist:
        print("Good job.")
    else:
        print('example 2')
        # example2()

example()
1 Like

Thank you! That worked for me as well, as you had them. However, if I adjust the code like so, the result is that the script quits no matter the result of random.choice(). Is there a reason for that?

import random

a = """foo1"""
b = """foo2"""
c = """foo3"""

mylist = [a, b, c]

def example():
    print (random.choice(mylist))
    if "foo1" in mylist:
        quit(0)
    else:
        example2()

def example2():
    raw_input("> ")

example()

I guess the answer is that " no matter the result"… “foo1” is always in your list.

Make sense?

Maybe you want to assign the result of “random.choice(mylist)” to a variable and validate if “foo1” is in that variable (and not in your list).

1 Like

That makes complete sense. Sorry for all the hassle when that solution was right in front of me the whole time. I tried it out and that completely solved the problem. Thank you so much for your help!

Don’t forget to share your game once ready, I’d like to try that

Will do! I’m hoping to have it ready within a week or so.