-2

Newbie needing help (only been learning for a week, so sorry if I get all the terminology wrong here)!

I'm trying to make a simple heads/tails game in Python 3 (using PyCharm). You start with a wallet and then make a guess and a bet. There's a random coin toss (using random integer generator). If the guess is right, the bet is added to the wallet (and vice versa).

Problem is that when I call the function I get the output I was expecting in the console, followed by 'None' on a separate line. I assume this is something to do with Boolean expressions but don't know what I'm doing wrong.

Here is the code

The console will print something like this (it's random so changes every time I run the program):

Correct, it was heads. Your wallet is now worth £200
None
Correct, it was tails. Your wallet is now worth £250
None

Does anyone know why I'm getting 'None' as well?

AMC
  • 2,642
  • 7
  • 13
  • 35
  • 1
    Can you please provide a version of your code, that I can copy and paste? We are happy to help, if you let us – nagyl Jul 06 '20 at 18:47
  • 2
    post the actual code as text, not a picture. do it quickly, because you are going to get downvoted into oblivion, even though your question isn't that bad – user120242 Jul 06 '20 at 18:47
  • just add return money to the end of your function – user120242 Jul 06 '20 at 18:49
  • Please do not share information as images unless absolutely necessary. See: https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors, https://idownvotedbecau.se/imageofcode, https://idownvotedbecau.se/imageofanexception/. – AMC Jul 06 '20 at 18:50
  • 3
    Does this answer your question? [Python Script returns unintended "None" after execution of a function](https://stackoverflow.com/questions/16974901/python-script-returns-unintended-none-after-execution-of-a-function) – AMC Jul 06 '20 at 18:51

2 Answers2

2

You are printing within the function and the value of the function. In the last line you print(heads_or_tails()) and since the function does not return anything it returns None. To fix this simply remove the print statement from around the function call at the end.

Cresht
  • 1,020
  • 2
  • 6
  • 15
  • Great - just tried this, thank you. Totally didn't think about the fact I was making it 'print' a 'print'. – Grace Di Méo Jul 06 '20 at 18:51
  • There are plenty of duplicates for this, like https://stackoverflow.com/questions/16974901/python-script-returns-unintended-none-after-execution-of-a-function. – AMC Jul 06 '20 at 18:52
  • it has nothing to do with printing a print, even though yes that would also cause None, since print returns nothing. your function doesn't return anything, thus it returns `None` – user120242 Jul 06 '20 at 18:54
  • @GraceDiMéo "printing a print" is hardly the most logical way of thinking about it. Loosely speaking, functions can do stuff like printing or they can be "pure functions", in which case they don't have any side effects like printing or mutating global variables. If you rewrite your function to take in `money` as the third parameter, remove `print` and actually `return` the updated value of `money` instead of changing a global variable, you'll get a pure function. Your current code kind of mixes the two. – Lev Levitsky Jul 06 '20 at 19:04
0

your function is not returning anything so to actually get the value of money you should execute the function and then just print(money) so it would be something like this :

heads_or_tails("head",100)
print(money)
heads_or_tails("tails",200)
print(money)
  • 1
    There are plenty of duplicates for this, like https://stackoverflow.com/questions/16974901/python-script-returns-unintended-none-after-execution-of-a-function. – AMC Jul 06 '20 at 18:52