# Games of Chance Challenge Project (Python)

Hi! This is late! To help with your NameError I suggest a try and except.

``````try coin_flip(heads, 20):
except NameError:
print('Must enter a string!')
``````

I don’t really use try and except, so don’t take the above code very seriously. Pretty sure I have a couple of errors in there.

Fantastic! There is no manipulation of the `money` variable in any of your functions, and they all return a bet, either negative or positive (or zero). I’ve not run it, but I love it all the same for sticking to the criteria. Well done!

1 Like

Hello everybody! I get stuck on my code, for now with a syntax error line 105 ( if guess == “black”: ) with the arrow showing the end of the line [ : ]… i really don’t understand the syntax error… Can someone brighten my lantern?

Here’s the full function:

`````` def roulette(guess, bet):
global money
ball = random.randint(0, 36)
red = [ 1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 ]
black = [ 2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35]
if guess == "even":
if ball % 2 == 0:
money += bet
print("You won " + str(bet) + " dollars! You now have " + str(money) + " dollars!")
else:
money -= bet
print("You lost, you now have " + str(money) + " dollars!")
if guess == "odd":
if ball % 2 == 0:
money -= bet
print("You lost, you now have " + str(money) + " dollars!")
else:
money += bet
print("You won " + str(bet) + " dollars! You now have " + str(money) + " dollars!")
if guess == "red":
if ball in red:
money += bet
print("You won " + str(bet) + " dollars! You now have " + str(money) + " dollars!")
else:
money -= bet
print("You lost, you now have " + str(money) + " dollars!"
if guess == "black":
if ball in black:
money += bet
print("You won " + str(bet) + " dollars! You now have " + str(money) + " dollars!")
else:
money -= bet
print("You lost, you now have " + str(money) + " dollars!"
if guess == ball:
money += bet*35
print("Wow! You won " + str(bet*35) + " dollars! You now have " + str(money) + " dollars!")
else:
money -= bet
print("You lost, you now have " + str(money) + " dollars!"
return money

print(roulette("red", 20))
print(roulette("odd", 20))
print(roulette(O, 20))
``````

Hi guys,

Here is my code of two of the games (Head&Tails and Cho-Han).
I’m having problems on how to change “money” to a global variable.

Cheers!

https://gist.github.com/c54abe88feedf1ebffe06556bf77e133

With errors like this, always check the line above as well:

``````print("You lost, you now have " + str(money) + " dollars!"
``````

using parentheses allows you to put your code on multiple lines, but this sometimes gives unexpected syntax errors if you forgot to close the parenthesis.

1 Like

You could use the `global` keyword? Although, that is generally something you want to avoid.

Wow, that was a beautiful SyntaxError, thanks a lot for the tip ! ^^

Global i used to check how it worked (long story, i was stuck on a while simply because of the scope as i declared the money variable out of any function). Except for the fact that chained bets and games can quickly ruin you, you mean it’s generally bad coding practice to systematically play with the scopes?

And… as i am around here, i tried to make some games more interactive, by using inputs instead of arguments in function definitions, all i could get was the EOFError. I was not able to understand how to get an input and avoid the EOF, is it a question of modules?

Here’s a sample of the head or tails game:

``````def flip():
global money
side = random.randint(0, 1)
guess = input("what's your guess? \n>")
bet = input("what's your bet? \n>")
if guess == "heads":
guess == 0
elif guess == "tails":
guess == 1
if guess == side:
money += bet
print("congratulations! you've won. You are going going to meet our management soles if you do that too much, though... You cashed " + str(bet) + " dollars.")
else:
money -= bet
print("welcome to losers land: don't worry anyone can play guitar and wear worn-off jeans, you can make money this way and come back when you've got some more to give. You've lost " + str(bet) + " dollars.")
``````

type or paste code here

updating global variable quickly makes the code difficult to read, you don’t know where the variable is updated. And some other function might change the global variable into something another function can’t handle. Which can lead to bugs

not all codecademy exercises handle `input()` equally well. What happens if you run your code at a site like repl.it?

… !!! Oh ok… well i still don’t know what it is about, but it took my input (thanks for the link), and i just had to change a few chunks to make it work, thanks a lot, stetim94 !

( in case someone needs it, here’s the skeleton for basic inputs and returns):

``````def flip():
money = 100
side = random.randint(0, 1)
guess = input("what's your guess? \n>")
bet = input("what's your bet? \n>")
print(side)
if (guess == "heads" and side == 0) or (guess == "tails" and side == 1 ):
print("you've won")
money += int(bet)
else:
print("you've lost")
money -= int(bet)
return money, bet

print(flip())
``````

I’m not really sure why the value for money kept resetting @ 100 every time I ran the program. If anyone knows why please let me know. Thanks!

Every time you run the program, the line `money = 100` is executed. This means that `money` starts off with the value `100` each time the program is run.

If you want `money` to hold the amount of money left after playing some games, you can save the value to a file. Or, you can incorporate more functionality into your program by using a loop and asking the user for inputs (which game they want to play, how much money they want to bet, etc.).

Thank you! I really appreciate your feedback.

1 Like

I really enjoyed this project. It’s a very good tryout on abstractions. I just had fun with this project. I tried as much as possible to simulate the gaming console experience with menu options and all that…

PS: I didn’t entirely follow instructions. Once I got the gist of what was expected, I just went off on my tangent. [I’m open to feedback].

Here is my work. I have worked around it a bit. It seems working for me on VS Code.