Games of Chance

I’m stuck on the heads or tails aspect of this project. I’m not sure what I’m getting wrong. Any help would be appreciated.

Edit: I changed all “current money” equations to have ‘==’. Still no success

you can’t use + to concatenate integer and string, i recommend using format method

or even better, f-string if your python version supports it


What about the current money variable? Theres an error saying its not defined, should I define it in an if else condition instead? Thanks for any help

I think you should make a decision about what should happen, and then once that’s done you can think about what you can leverage from the language to make that happen.

So, what should happen there, and how does that relate to the error message that you’re getting? Do you agree with the error message?

Something you will want to avoid (because I see you do it) is making a guess about what something does, using it, and hoping it’ll do the right thing. You need to know what the things you use do, otherwise you can’t leverage them.

You can also try things out in isolation from other code. Whatever action you’re currently working on, get it to function by itself without anything else in the same file. Once it’s behaving like you want (and not by accident, deliberately, able to argue for everything in it), then you can add it to the rest of the code.

Your error message. What is it saying? It’s saying something isn’t defined. Why would python say that? It would say that if you’re trying to use something that does not exist, for example:


To fix this, you would want to either stop using the thing:

print('something else')

Or ensure it’s there.

x = 3

So now you should be asking yourself things like:

Did I mean for this variable to already exist?
Am I trying to read this variable, that is after all what would trigger this error message.
Do I want for this variable to exist?

1 Like

Thanks! I’ve defined current money, but the function isn’t working quite how I anticipated. When i run the code it re-starts the initial value at 100, even if i want it to incorporate the remaining money from the first coin toss. Also is there an intrinsic issue with the code that would lead it to always execute the ‘else’ function bc the other conditions are poorly defined?

There’s quite a lot going on so there’s no straight answer.

Variables in a function are private to that function (and if the function is called several times simultaneously then each instance is separate)
So when you do
current_money = ...
Then that variable is private to that function.
There’s another variable by the same name that you’ve defined globally. That’s a different variable.

A function should usually be isolated from any outside parts, ideally should only communicate through the arguments it receives and the return value it produces at the end. It shouldn’t touch a global variable.
You can. But it’s an escape hatch that, while it solves the problem here, becomes a less and less useful solution as code grows.

But that also says something about what a solution can be. Send in money as an argument, send out new money as a return value. If you have multiple things that you want to return, then bundle them up into a single value composed of several values. Usually you’d use a tuple for this.

# this is what I think you should be doing for the moment
money = 100
money = heads_or_tails(heads, 3, money)
money = heads_or_tails(tails, 3, money)

Another option is to create some form of context. Not a global variable, you can make as many contexts as you want, but also not something you have to manually move around as input and output.

So you might create a context holding 100 money, and then within that context call your function multiple times, and the money in the context would change.

Classes are one way to go about this. Another is is to use the private variables of a function. Something like:

wallet = Wallet(100)
wallet.heads_or_tails(heads, 3)
wallet.heads_or_tails(tails, 3)

The money sits in the wallet, isolated, yet doesn’t have to be passed around. Okay that’s a lie, you’re passing around the wallet instead. But if there are multiple values to pass around then passing around one wallet might be a whole lot nicer. And, you don’t have to manage updated wallets, it’s the same wallet, just one object, easier to handle.

In particular, you can now effortlessly have a thousand wallets, even different kinds of wallets. You’d have problems doing this with global variables. You might not even have only wallets, but other things as well. Each one is self-contained, isolated from all the others.
Storing just money as integer is totally fine too, you can have a list of thousands of integers, that’s fine. But it’s not fine if they’re global variables.

1 Like