0

I'm working on a code for a 'casino' in Python (no GUI yet, just trying to get the codes down for now). I want coins to be a system of currency, and I have a perfectly fine Russian Roulette code. If the player survives, I want the code to add onto the player's coins. Said coins are already defined at the top of the code, outside of the function. When I try

return coins += 100

at the elif clause for the player's survival, I immediately get "invalid syntax" in IDLE. How can I have the function modify 'coins'?

2
  • 3
    coins are already defined at the top of the code, outside of the function this can be, but is usually not a good idea. Commented May 21, 2014 at 13:36
  • removed IDLE tag since this has nothing to do with using the IDE itself. Commented May 21, 2014 at 13:46

3 Answers 3

2

Make sure that you're using coins as a global variable:

>>> coins = 0
>>> 
>>> def f():
...     global coins  # <--
...     coins += 100  # notice also that we're not returning anything
... 
>>> f()
>>> 
>>> coins
100
Sign up to request clarification or add additional context in comments.

Comments

0

Try return 100 And the caller code would be like coins += russianRoulette() That should work

1 Comment

... if, e. g., russianRoulette() raises and exception on "deadly hit".
0

Use coins as a function parameter then return that value with the addition:

coins=2

def f(coins):
    return coins+100

>>> f(coins)  
102

Or just assign coins the value returned:

>>> coins=f(coins)
>>> coins
102
>>> coins=f(coins)
>>> coins
202

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.