Codecademy Forums

Why does str(x) return None when called?

Link to the relevant exercise

My code:

def coin_flip(guess, bet):
coin = random.randint(1,2)
if guess is “Heads” and coin is 1:
print (“You won " + str(bet) + " dollars.”)
elif guess is “Tails” and coin is 2:
print (“You won " + str(bet) + " dollars.”)
elif guess is “Heads” and coin is 2:
print (“You lost " + str(bet) + " dollars.”)
else:
print (“You lost " + str(bet) + " dollars.”)

The code works, it just returns a None. value along with the “You lost/won x dollars.”. How do you eliminate the none value returning when using str()?

str is the type for strings. when you call it, you get a string. you got a string. not none. maybe you converted None to string which would contain the letters N-o-n-e. Anything you’re printing is string, it’s never the value itself (your console displays text)

I’m not following. When I print(coin_flip(“Heads”, 20)), The console returns:

You lost 20 dollars.
None

Maybe I’m using the wrong terminology. Just trying to determine what is causing the None to be there.

How many output lines do you expect? How many times do you call print? You’ve got print in many places in your code, if you expect to print one thing then shouldn’t there also be only one use of print? (If you only execute one of them then yeah that would be only one, but that’s not the case, is it)

I’m hoping to get one line returned when I call the function coin_flip, “You won/lost X dollars”. I simplified the code using an or statement but am still getting None on a second line:

def coin_flip(guess, bet):
coin = random.randint(1,2)
if guess is “Heads” and coin is 1 or guess is “Tails” and coin is 2:
print (“You won " + str(bet) + " dollars.”)
elif guess is “Heads” and coin is 2 or guess is “Tails” and coin is 1:
print (“You lost " + str(bet) + " dollars.”)

Your function does not include a return statement. The behaviour you’re seeing is nothing to do with your use of the str() function.

By default, if no return value is specified, Python will return None.

Hence, when you’re doing print(coin_flip("Heads",20)) you’re printing the return value from your function which is ‘None’.

If you just want to call the function, which will still output the win/lose bits to the console, drop the print() wrapper around your call to coin_flip()

2 Likes

That makes sense, and solved the issue. Thank you!

1 Like

No problem.

If you wanted to do it the other way, i.e. have something come back which you can print like print(coin_flip("heads",2)):

from random import randint

def coin_flip(guess,bet):
  coin = ["heads","tails"]  # use a list to store the outcomes
  ht = randint(0,1)         # decide heads or tails
  flip = coin[ht]           # store the result of the flip
  
  if guess.lower() == flip.lower(): # we can directly compare the guess to the outcome, shortening the code
    return ("won",bet) # if they match, the player won.
  else:
    return ("lost",bet)# if they don't match, they lost.

# lets run a little loop to have a couple of goes.
tries = 5
while tries > 0:
  print("You %s %s dollars!" % coin_flip("heads",2)) # print the outcome
  tries -= 1
else:
  print("You've run out of tries.")

The result on the console is:

You won 2 dollars!
You lost 2 dollars!
You won 2 dollars!
You won 2 dollars!
You won 2 dollars!
You've run out of tries.

:slight_smile:

2 Likes