Games of Chance Challenge Project (Python)

Hi @ajax1291381554!

Why are you dividing the value of player_bet (which in this case, the value looks like “even”) by 2? It seems like you’re trying to divide a string by a number which would give you a syntax error.

actually it doesnt, and thats the syntax for even and odd. but its any saying i lossed when i call the function, im trynna see whats woring with the function

The error message code doesn’t even reflect the code in the editor. Line 42 in the editor doesn’t close 'even string, while in the error message it does ('even')

then on line 28, you overwrite the value of your player_bet parameter, so now you no longer no know if the user went for 'even' or 'odd'. You can just use strings in the comparison?

also, The is operator/keyword compares the identity of two objects. Is that really what you want to do?

1 Like

guess = random.randint(1,2)

def dice(guess,player_guess,bet):
while True:
if player_guess == guess:
return "you won " + str(bet)
break
else:
return "you lossed " + str(bet)

def cho_han(bet,even,odd,):
dice_roll1 = random.randint(1,6)
dice_roll2 = random.randint(1,6)
sume = dice_roll1 + dice_roll2
print(sume)
if (sume%2==0) and (even == True):
return "you won " + str(bet)
elif (sume%2==1) and odd is True:
return " you won " + str(bet)
else:
return " you lossed " + str(bet)

print(cho_han(30,even=False,odd=True))

#Call your game of chance functions here

Code but no context, what about it?

A post was split to a new topic: Game of change challenge project

I’m struggling with making the code accumulate/reduce money from the starting amount. E.g. If I run the code 3 times with 10 bets and he loses 3 times, it should say his money is at 70. How can I get around this?

import random
                          

#Write your game of chance functions here
money = 100

    
def coin_flip(guess,bet):
  if bet <= 0:
    print("-----")
    print("Please bet higher.")
    return 0
  print("Coin is being flipped. You guess " + guess +".")
  print("-----")
  result = random.randint(1,2)
  if result == 1:
    print("Heads.")
  elif result == 2:
    print("Tails.")
  if (guess == ("Heads" or "heads" or "Heads!"or "heads!"or "head") and result == 1) or (guess == ("Tails" or "tails" or "tail" or "Tails!" or "tails!") and result ==2):
    print("You won " + str(bet)+" dollars!")
    print("--")
    return bet
  else:
    print("You lost " +str(bet)+" dollars. Better luck next time.")
    print("--")
    return -bet

money += coin_flip("Heads", 10)
print("Your total amount of money is $" + str(money) + ".")

OR

import random
                       
#Write your game of chance functions here
money = 100
num = random.randint(1,2)

def coinflip(guess, bet):
  if guess == num:
    money2 = money + bet
    return "You won $"+ (str(bet))+ "!" + " This brings your total bankroll to $" + str(money2) + "!"
  
  else:
    money2 = money - bet
    return "You lost -$"+ str(bet)+ "!" + " This brings your total bankroll to $" + str(money2)  + "!"
print(coinflip(1, 20))

by running the code 3 times, do you mean: press the run/save button in the challenge three times?

Active programs run in memory (RAM), once the program is finished the memory is released back to the OS. To solve this problem, you would:

need to persist the data after being done running
do multiple coin flips within the lifetime of the program

Here’s my code for roulette. I’m looking for suggestions on how to make it more concise. In particular:
I couldn’t figure out a way for the input function to accept either an integer or a string, so I converted all of my integer lists into strings. Is there a way to have it accept either?
I did a lot of copy and paste for the bet payout and play again option, because it was the same for each case. Is there a way to define that as a function that can be called where needed in the code?
Thank you!

https://gist.github.com/jennie8902/cca2bbb8c95d86fd63355bfb024bef6a

download winrar and then extract.

when i am trying to make repository its not coming. i am clicking C drive -> git bash here-> get init ->enter but no .git folder is created???

Hi everyone, here is my project I’ve completed for this and would be great to receive feedback as well for this :slightly_smiling_face:

Hi! My Code is here:

Here is my solution! Hope it helps!

I tried to make the challenge not just a demonstration of doing functions that worked, but to make an actual game.

This is my first program, so I would love to receive feedback, suggestions or opinions.

What matters is to improve :fire:

My first project! I changed up the rules of roulette here and there, and I haven’t added any try-except blocks, but let me know what you think of my code.

https://gist.github.com/bc5438e76a81e4ebec549058640b85bf

Codecademy Project #1 - Games of Chance
 games_of_chance.py
import random, sys

money = 100
print('You will start the day with $100!\nHAVE FUN!!\n')


''' List of 4 game functions '''

# Heads or Tails coinflip
def coinflip(bet, choice):
    global money
    if choice.lower() == 'heads':
        choice = 0
    else:
        choice = 1
    pc_flip = random.randint(0, 1)

    if pc_flip == choice:
        money += bet
        print(f'You won ${bet}!')
        return f'You now have ${money} in your wallet.\n'
    else:
        money -= bet
        print(f'You lost ${bet}!')
        return f'You now have ${money} in your wallet.\n'


# Cho-han, adding 2 dice and guessing if that number is Odd or Even
def chohan(bet, choice):
    global money

    if choice.lower() == 'even':
        choice = 0
    else:
        choice = 1
    pc_flip = random.randint(1, 6)
    pc_flip2 = random.randint(1, 6)
    pc_flip_total = pc_flip + pc_flip2
    pc_flip_added = (pc_flip + pc_flip2) % 2
    print(f'Dealer flips and gets {pc_flip_total}!')
    if pc_flip_added % 2 == choice:
        money += bet
        print(f'You won ${bet}!')
        return f'You now have ${money} in your wallet.\n'
    else:
        money -= bet
        print(f'You lost ${bet}!')
        return f'You now have ${money} in your wallet.\n'


# Pulling cards from a regular deck
# The person with the highest number wins
# Suit doesn't matter
def card_pick(bet, choice1, choice, suit):
    global money

    pc_card1 = random.randint(1, 13)
    pc_card = pc_card1
    if pc_card1 == 1:
        pc_card = 'Ace'
    elif pc_card1 == 11:
        pc_card = 'Jack'
    elif pc_card1 == 12:
        pc_card = 'Queen'
    elif pc_card1 == 13:
        pc_card = 'King('
    else:
        pc_card = pc_card1

    pc_suit1 = random.randint(1, 4)
    if pc_suit1 == 1:
        pc_suit = ' of Spades'
    elif pc_suit1 == 2:
        pc_suit = ' of Clubs'
    elif pc_suit1 == 3:
        pc_suit = ' of Hearts'
    else:
        pc_suit = ' of Diamonds'

    print(f'Dealer pulls {pc_card}{pc_suit} ... Player pulls {choice}{suit}')
    if choice1 < pc_card1:
        money -= bet
        print(f'You lost ${bet}!')
        return f'You now have ${money} in your wallet.\n'
    elif choice1 > pc_card1:
        money += bet
        print(f'You won ${bet}!')
        return f'You now have ${money} in your wallet.\n'
    else:
        return f'It was a tie! No money gained or lost.\n'


# Roulette, with the rules tweaked here and there
def roulette(bet):
    global money

    print('Are you betting on:\n')
    print('[1] color\n[2] odd/even\n[3] number\n[4] number + color ')
    choice = int(input('Please make your selection: '))

    pc_roll = random.randint(0, 37)
    if pc_roll == 37:
        pc_roll = 00
    pc_color = random.randint(0, 1)
    if pc_color == 0:
        pc_color = 'black'
    else:
        pc_color = 'red'

    if pc_roll == 0 or 00:
        print(f'Spinning ... {pc_roll} {pc_color}!')
        money -= bet
        print(f'You lost ${bet}! (0 and 00 always loses)')
        return f'You now have ${money} in your wallet.\n'

    if choice == 1:
        color = input('[Red] or [Black]? ')
        print(f'Spinning ... {pc_roll} {pc_color}!')
        if color.lower() == pc_color:
            money += bet
            print(f'You won ${bet}!')
            return f'You now have ${money} in your wallet.\n'
        else:
            money -= bet
            print(f'You lost ${bet}!')
            return f'You now have ${money} in your wallet.\n'

    if choice == 2:
        odd_even = input('[Odd] or [Even]? ')
        print(f'Spinning ... {pc_roll} {pc_color}!')
        if odd_even.lower() == 'even':
            if pc_roll % 2 == 0:
                money += bet
                print(f'You won ${bet}!')
                return f'You now have ${money} in your wallet.\n'
            elif pc_roll % 2 == 1:
                money -= bet
                print(f'You lost ${bet}!')
                return f'You now have ${money} in your wallet.\n'
        else:
            if pc_roll % 2 == 1:
                money += bet
                print(f'You won ${bet}!')
                return f'You now have ${money} in your wallet.\n'
            elif pc_roll % 2 == 0:
                money -= bet
                print(f'You lost ${bet}!')
                return f'You now have ${money} in your wallet.\n'

        if odd_even.lower() == 'odd':
            if pc_roll % 2 == 1:
                money += bet
                print(f'You won ${bet}!')
                return f'You now have ${money} in your wallet.\n'
        elif odd_even.lower() == 'even':
            if pc_roll % 2 == 2:
                money -= bet
                print(f'You lost ${bet}!')
                return f'You now have ${money} in your wallet.\n'

    if choice == 3:
        number_choice = int(input('\nChoose a number between 1 and 36: '))
        print(f'Spinning ... {pc_roll} {pc_color}!')
        if number_choice == pc_roll:
            money += bet * 2
            print(f'You won ${bet * 2}!')
            return f'You now have ${money} in your wallet.\n'
        elif number_choice != pc_roll:
            money -= bet * 2
            print(f'You lost ${bet * 2}!')
            return f'You now have ${money} in your wallet.\n'

    elif choice == 4:
        number = int(input('Choose a number between 1 and 36: '))
        color = input('Choose a color: ')
        print(f'Spinning ... {pc_roll} {pc_color}!')
        if number == pc_roll and color != pc_color:
            money += bet * 2
            print(f'You won ${bet * 2} on half!')
            return f'You now have ${money} in your wallet.\n'
        elif number != pc_roll and color == pc_color:
            money += bet * 2
            print(f'You won ${bet * 2} on half!')
            return f'You now have ${money} in your wallet.\n'
        if number == pc_roll and color == pc_color:
            money += bet * 4
            print(f'You won ${bet * 4}!')
            return f'You now have ${money} in your wallet.\n'
        elif number != pc_roll and color != pc_color:
            money -= bet * 4
            print(f'You lost ${bet * 4}!')
            return f'You now have ${money} in your wallet.\n'


''' Checking if any money remains for betting '''

while True:
    if money <= 0:
        print('Sorry, you have run out of money.\n')
        print('Please return to the front desk if you have more money.\n\n')
        print('THANK YOU FOR YOUR PATRONAGE!!')
        sys.exit()

    ''' Checking if the player wishes to leave with their current earnings '''

    quitting = input('Would you like to leave with your earnings? [Yes] or [No] ')
    if quitting.lower() == 'yes':
        print(f'Today, your earnings total ${money - 100}, for a total of ${money}!')
        print('WOW!\n\nTHANK YOU FOR YOUR PATRONAGE!')
        sys.exit()

    ''' Game of Chance functions '''

    print('\nChoose a game:\n''[1] Coinflipper\n[2] Cho-han\n[3] Cardpicker\n[4] Roulette\n\n')
    game_choice = int(input('Enter your choice: '))
    bet = int(input('How much do you want to bet: '))

    # Coinflip
    if game_choice == 1:
        choice = input('Did you get Heads or Tails? ')
        print(coinflip(bet, choice))

    # Cho-han
    if game_choice == 2:
        choice = input('Will the added dice be [Odd] or [Even]? ')
        print(chohan(bet, choice))

    # Cardpicker
    if game_choice == 3:

        # Dealer card pull
        # Card number or face
        choice1 = random.randint(1, 13)
        choice = choice1
        if choice1 == 1:
            choice = 'Ace'
        elif choice1 == 11:
            choice = 'Jack'
        elif choice1 == 12:
            choice = 'Queen'
        elif choice1 == 13:
            choice = 'King'
        else:
            choice = choice1

        # Dealer suit pull
        # Only for realism
        suit = random.randint(1, 4)
        if suit == 1:
            suit = ' of Spades'
        elif suit == 2:
            suit = ' of Clubs'
        elif suit == 3:
            suit = ' of Hearts'
        else:
            suit = ' of Diamonds'

        print(card_pick(bet, choice1, choice, suit))

    # Roulette
    if game_choice == 4:
        print(roulette(bet))


All I can say is, great job. You’ve learned to tie your shoelaces. Now the real task is to refactor the code. Can you pare down the logic and control to something modular and recognizable? That would be without video support.

1 Like

Not sure what you mean by video support…this project was for people that finished the first 3 sections of Codecademy, so I did it with the knowledge I have so far.

1 Like

Ah, so there is no video? Okay, my bad. Still, with what you know can you find ways to refactor your code? I can, without going beyond what concepts are present. Bookmark this exercise and return to it in a couple of weeks.

1 Like

my game of chances project.
any feedback would be appreciated.