Python Games Of Chance

Hello.
I am doing a little extra for the Games of Chance project (https://www.codecademy.com/practice/projects/games-of-chance). I have done everything correctly, and when I run the functions they all work. Here the last part of my code, which is throwing the error: name 'play' is used prior to global decleration. This is in relation to my while loop.
Here it is:

global play
play = 'yes'
while play == 'yes':
    game = input('Which game? 1 for heads or tails. 2 for Cho Han. 3 for card pick. 4 for roulette')
    if game == 1:
        global play
        coin_flip()
        print(money)
        play = input('Do you want to keep playing Game of Chance. Type yes if you do. Else type anything')
    elif game == 2:
        global play
        Cho_Han()
        print(money)
        play = input('Do you want to keep playing Game of Chance. Type yes if you do. Else type anything')
    elif game == 3:
        global play
        card_pick()
        print(money)
        play = input('Do you want to keep playing Game of Chance. Type yes if you do. Else type anything')
    elif game == 4:
        global play
        roulette()
        print(money)
        play = input('Do you want to keep playing  Game of Chance. Type yes if you do. Else type anything')
else:
    print('You have %f dollars. Thank you for playing games of chance!' % money)
        

Thank you sincerely!

global isn’t magic fairy dust that you can sprinkle on to make things better

remove it

Thank you for your time!
However, when I removed global from the code, it did not run properly. it left me with just this(in the console, when I ran the code):

Which game? 1 for heads or tails. 2 for Cho Han. 3 for card pick. 4 for roulette
1
Which game? 1 for heads or tails. 2 for Cho Han. 3 for card pick. 4 for roulette
1
Which game? 1 for heads or tails. 2 for Cho Han. 3 for card pick. 4 for roulette
1

When I press 1, for heads or tails, it just loops straight back to asking my what game I want. Also, I have tried using the global statement at the beginning of the while loop, and just at the beginning of the if loop. It threw the same error. Why does it do this?
Thank you sincerely.

What your code is saying is: if there’s no match, do it again, so if that is happening, that would suggest there’s no match between the values you compared. What values did you compare?

Do you think it should do something else? What effect would global have? If you don’t yet know what it’s supposed to do then isn’t it a bit premature to ask why it does whatever it does? Before that, find out what it does.

Thank you! I was using int type for my control statements instead on str! Thank you again for your time!

    if game == 1:
        global play
        coin_flip()
        print(money)
        play = input('Do you want to keep playing Game of Chance. Type yes if you do. Else type anything')
    elif game == 2:
        global play
        Cho_Han()
        print(money)
        play = input('Do you want to keep playing Game of Chance. Type yes if you do. Else type anything')
    elif game == 3:
        global play
        card_pick()
        print(money)
        play = input('Do you want to keep playing Game of Chance. Type yes if you do. Else type anything')
    elif game == 4:
        global play
        roulette()
        print(money)
        play = input('Do you want to keep playing  Game of Chance. Type yes if you do. Else type anything')

If you have several things, a list is usually good for holding on to them. You’ve got several games. You could have a list of games instead

Yes, that would be a better idea! Thank you!

I’m guessing global was something your game functions were using?
They could accept money as an argument and return a new amount instead, getting rid of global entirely.

I made a list like this:

money = 100
play = True
games = [
    ("heads or tails", coin_flip),
    ("Cho Han", Cho_Han),
    ("card pick", card_pick,),
    ("roulette", roulette),
]
menu = " ".join(f"{n} for {name}." for n, (name, game) in enumerate(games, 1))

What’s unique about each entry is the name and the function, so that’s what goes into the list. The menu string can be created from that list, and the code for choosing a game would use it too - so adding or removing games can be done in that list with no other changes.

Oh and I see you format money as a float. Consider counting cents instead, since you are operating on exact amounts of cents, not an approximation (floats are not suitable for representing numbers with a decimal component unless you mean to approximate them) You would obtain the dollar amount by dividing by 100 and the cents are the remainder of that division.

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.