Question about Chance Game

Hello, I am currently in the starting stages of learning python through codeacademy. I am working on the chance game which I was able to make work fine. py1

While this game does work fine I do have some questions.

  1. I notice when I return new_total and just return in my function my game still works the same. What is the difference when I return new_total or just simply use return.

  2. As you can see for my else statement I added a little extra saying if the bet was more than the money you cannot bet more money than what you have, this code also works just fine. However I tried to add code that tells the user they did not enter a valid guess.

elif guess != “Heads” or “Tails”:
print(“This is not a valid guess! Please guess Heads or Tails”)
return

Note I had this code at the very bottom of my function right before the last else:

This code seems to break my program, All of my outputs are claiming invalid guesses even when a guess is not invalid, can you tell me what I am doing wrong? Thank you for the help!

What does return at all do and does the user of your function expect that to happen?

guess != "Heads" or "Tails"

How many comparisons using != are you hoping to make there? There’s only one use of != so there’ll only be one such comparison.

You have two operators there != and or, what will they receive as input in your expression?

You might find that more straght-forward to reason about if you were to use those operators as functions instead.

noteq(a, b) or(a, b) how would you write the above expression using functions like that?

or(noteq(guess, "Heads"), "Tails")

Likely, you were hoping python would understand what you meant and didn’t quite think through what you wrote. But everything already has a fixed behaviour, it’s not going to do what you want, it’s going to do what it promises to do, so, you have to consider what it promises and then carefully leverage that to get what you want.

I understand the != issue now and have fixed, That was a dumb mistake.
I also understand that returning returns the value and ends the function but I still dont get the difference for just return or return new_total since both seem to work in my code just fine.

I wouldn’t call it dumb, but definitely the wrong expectation, yeah. There do exist quite a lot of things to avoid repetition in such expressions, but you would still need that promise in order to expect it to take effect.

If something makes no difference it’s because you’re ignoring the difference isn’t it? So what does it do, and would you argue that you use that? If you don’t argue that you use it, then you can’t really say that there should be a difference. Or if you do argue that you use it then you’ve shown that you really don’t, and then you’d have to look closer at the use of it.

In the mathematical sense, a function can communicate only through the arguments it receives, and the return value that it produces. It can have no other effect, can’t communicate in any other way in any direction.

Some functions may have constant results, so they would need no input, for example: 5

But a function that has no output is rather useless.

Your function however, does not adhere to those rules, it’s doing communication on the side, so it can be useful without having a return value.

It can still have a return value. The question is, do you expect it and/or need it?

So whether it should make a difference is something you, the author of your code, should be answering.

It’s the same thing as with the operators: Hey, what does this do, and how can I leverage that?

It’s also often useful to study the mechanics of things in isolation of other things, so you might for example make two functions where one returns something and the other doesn’t, and that would be the only thing those functions do, then you could call the functions to see how they behave. Same for debugging if something has decided to be a mystery, look at it with less code involved, start removing things until there’s just the mysterious part left to look at.