Python 3 Project: Games of Chance (Heads or Tails)

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

Hello code ninjas! I am fumbling through the Games of Chance project and have become stuck. My code executes successfully and completes a round of betting for the Heads or Tails game. However, I cannot figure out how to achieve a running money outside the function so that players can play multiple times and see their wallets grow (or shrink :wink: ).

My code is as follows:

import random

money = 100

#Write your game of chance functions here
def heads_or_tails(guess, bet):
  num = random.randint(1,2)
  money = 100
  winnings = 0
  while money > 0:
    if num == 1:
      print('Heads!')
      if num == guess:
        print('Winner!')
        winnings += bet * 1.5
        print('Winnings: ' + str(winnings))
      else:
        print('Better Luck Next Time!')
        winnings -= bet
    elif num == 2:
      print('Tails!')
      if num == guess:
        print('Winner!')
        winnings += bet * 1.5
        print('Winnings: ' + str(winnings))
      else: 
        print('Better Luck Next Time!')
        winnings -= bet
    else:
      print('Error! Bet refunded.')
    money += winnings
    print('Money left: ' + str(money))
    return True

Hello @kingxmont, welcome to the forums! Instead of directly answering you, I’ll pose some questions that should help you:

  1. Do you know exactly what you want? (i.e. the interface, the way money reacts, etc)
  2. If so, can you break down what that would mean?
  3. Once you’ve broken it down, write out instructions for yourself. (not a question, but still…)
  4. Can you, following the instructions that you wrote, build a small script somewhere else that adds “money” based on some logic?
  5. Once you’ve done this, can you implement that code into your actual script?
Once you've done that, you should be able to do it, however here are some more specific questions
  1. How do you get a global variable to interact with locally scoped variables?
  2. Would using a parameter, or return be more useful here?
  3. How would you call the function in such a way that it interacts with the money variable?

I hope this helps!

2 Likes

Lots of food for thought, thank you!
My thoughts:
To be honest, I’m getting overwhelmed with this project. I’m having trouble breaking down all the aspects of what the project requires. My guess at this point would be to define another function that changes money with the value returned from the Heads_or_Tails function, but I’m still not sure how to update the global variable. The project gives you the variable but does not make it global so I could be getting stuck in a incorrect frame of mind, or I, in fact, do need a global variable for wallet; especially if I start having multiple possible games. :worried:

2 Likes

The games of chance should all return the same thing, a positive or negative number, or zero. Work with the return value and keep money out of the games.

3 Likes