Game of Chance

Hello,

my first post here. I am trying out Python and I am stuck with “Game of Chance”.
I realize that it is very very simple. But I am trying to make it first and increase the difficulty later in the next step, using e.g. while loops and so on. So I am not looking for a complete new solution. My question concerns the code below.

When I run it it gives me either 85 (loss) or 115 (win) which is correct. However the print statements give me a hard time. It does not print what I want it to print. Whatever I print() is not printed at all. And I do not understand why it prints 85 or 115 even when there is no print() statement at all.

This must be very noobish for you. But still I would be thankful for an answer.

Thank you,
Fabian

Link to project:
https://www.codecademy.com/practice/projects/games-of-chance

Code:

import random



#Write your game of chance functions here

def coinFlip (guess, bet):
  flip = random.randint(1,2)
  money = 100
  if flip == guess:
    money += bet
    return money
    print("something")

  if flip != guess:
    money -= bet
    return money
    print("something else")

#Call your game of chance functions here
start = coinFlip(1,15)
print(start)

Hello, @falux, and welcome to the forums.

Consider for a moment what return does. Computers execute commands in a specified order. We refer to that as control flow. If you place your index finger at the top of the program, then move to the line that is executed first, and then follow the control flow, when would the print statements that follow your return statements be executed?

@midlindner
Thank you! A simple rule.

So in the code above I switched the order of “return” and “print” to

print(“something”)
return money

And it works :smiley:

Thank you for helping!
Fabian

1 Like