Games of Chance Challenge Project (Python)

Why is it that this code, which works in the Python IDLE, does not work in Code Academy? I’m confused.

import random
money = 100
guess = input(‘Enter H for Heads or T for Tails:’)
bet = int(input('Place your bet: '))
#Write your game of chance functions here
def coin_flip(guess, bet):
flip = random.randint(1,2)
if guess == ‘H’ and flip == 1:
money1 = money + bet
print('You won! You now have '+ str(money1) + ‘!’)

elif guess == ‘T’ and flip == 2:
money1 = money + bet
print('You won! You now have '+ str(money1) + ‘!’)

else:
money1 = money - bet
print('You lost. You now only have '+ str(money1) + ‘.’)

#Call your game of chance functions here
coin_flip(guess, bet)

It works perfectly in the Python IDLE, but doesn’t work at all within the Code Academy learning environment. This is the error I get in Code Academy:
Enter H for Heads or T for Tails:Traceback (most recent call last):
File “script.py”, line 3, in
guess = input(‘Enter H for Heads or T for Tails:’)
EOFError: EOF when reading a line

I know the code is good, because it works in Python’s IDLE. It works perfectly. Why does it not work in Code Academy?
Also, does anyone know why Python’s IDLE won’t let me put “money = money + bet” or “money = money - bet”? I had to change it to the “money1 = money + bet” and “money1 = money - bet” like above.

It’s not IDLE, but Python that will not permit assignment to a global from inside a function. There is no binding, but read only access to the global. To create a binding that will permit writing to the global, declare it in the first line of the function…

def func(args):
    global var
    var += value
    # ...

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: