Game of Chances (coin flip) question

Question:
If I want to prompt a player to input their “bet” and/or “wage” in the code below, how would I do that. My code works, except when I attempt to gain input from user. Please see line beginning “val =”.

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

Thanks


import random
money = 100
num = random.randint(1, 2)

#Write your game of chance functions here
def coin_flip(bet, wage):
  num = random.randint(1, 2)
  if bet == "Heads" and num == 1:
    balance = money + wage
    val = input(f"What is your {bet} and {wage}:")
    print(val)
    print(f"You won ${wage}!) ")
    print(f"You went from ${money} to ${balance}")
  if bet == "Tails" and num == 2:
    balance = money + winloss
    print(f"You bet ${wage}")
    print(f"You won ${wage}! ")
    print(f"You went from ${money} to ${balance}")
  else:
    balance = money - wage
    print(f"You bet ${wage}, but you lost.")
    print(f"Pay out ${wage}.")
    print(f"You went from ${money} to ${balance}")
    
    

coin_flip("Heads", 15)
coin_flip("Tails", 22)
coin_flip("Heads", 45)

i would make a separate function for getting the user input

this function should contain two prompt: one for the bet and one of the wage, then you can validate each input. Letting the user input two things at one is a difficult thing to do

ideally, make 3 functions: coin_flip, get_input and main.

then your main function looks something like this:

def main():
   user_input = get_input()
   coin_flip(user_input)

this also gives you the possibility to let the user play multiple times

If you have further questions (which is possible), please let me known and include an updated version of your code

Thank you for taking the time to respond. There are several components to your suggestion and my code (see below) that I still do not understand.

First, would the “main” function be written as you suggested? If so, how would I call it later in the program. Second, can I have a function within a function? And third, what am I doing wrong with my code?

Please forgive me for so many questions. Any additional help will be appreciated.
Thanks!


def main():
def get_input():
user_input1 = print(input(“Heads or Tails?”))
return user_input1
user_input2 = print(input(“What is your wager?”))
return user_input2
def coin_flip(bet, wager):
num = random.randint(1, 2)
if bet == “Heads” and num == 1:
balance = money + wager
print(f"You bet {wager}, and you won!") print(f"You went from {money} to {balance}") if bet == "Tails" and num == 2: balance = money + wager print(f"You bet {wager}")
print(f"You won {wager}! ") print(f"You went from {money} to {balance}") else: balance = money - wager print(f"You bet {wager}, but you lost.")
print(f"Pay out {wager}.") print(f"You went from {money} to ${balance}")

main()

By separating functionality, your program becomes easier to read, is better maintainable, easier to test and its all-round better design.

In this case i wouldn’t nest function, i would go for:

def get_input():
   // body of function

def coin_flip():
   // body of function

def main():
   // call get_input and  coin_flip here

main()

In my previous example, i showed the function calls within the main function.

Few other problems:

Once a return keyword is reached, the function exits. It has handed back data, so its done. So this:

user_input1 = print(input(“Heads or Tails?”))
return user_input1
user_input2 = print(input(“What is your wager?”))
return user_input2

won’t work. You could use a list or a dictionary:

return [user_input1, user_input2]

also here:

user_input1 = print(input(“Heads or Tails?”))

this is a problem. Now the input gets printed, but the print() function itself doesn’t return anything, so user_input1 is now None (the absence of a return value)

1 Like

Thank you!!! I will work on code and send back results.

Hello Friend,
Couple points:

  1. I could not get the input() function to properly return a prompt for user to input response (don’t know if Codecademy environment restricts that action).

  2. I am really struggling understanding the following concepts and usages: a) when/where to properly use quotes (other than in print() functions and around strings); b) when/how to properly employ Return (with or without print() function).

  3. I am having another issue with Cho Han portion of exercise. My code will not return literal string portion of “Odd” code as written, nor will it distinguish even and odd numbers.
    Please advise when time permits.
    Thanks in advance.

# Game of Chances - Cho Han 
def cho_han (play_choice, wager):
  num1 = random.randrange(1, 7)
  num2 = random.randrange(1, 7)
  roll_dice = num1 + num2
  play_choice = "Even" or "Odds"
  
  # Setting up code for "Even"/"Odds" and roll_dice attributes
  if play_choice == "Even" and roll_dice == 2 or 4 or 6 or 8 or 10 or 12:
    print(f"The dice rolled {roll_dice}... An even!")
    print(f"You won ${wager}!")
  elif play_choice == "Even" and roll_dice != 2 or 4 or 6 or 8 or 10 or 12:
    print("Ahh...wrong bet, pay up ${wager}.")
  elif play_choice == "Odds" and roll_dice == 3 or 5 or 7 or 9 or 11:
    print(f"The dice rolled {roll_dice}... Odds!")
    print(f"You won ${wager}!")
  elif play_choice == "Odds" and roll_dice != 3 or 5 or 7 or 9 or 11:
    print("Ahh...you picked Odds! Pay up ${wager}.")
  else:
    print(f"Sorry...the dice rolled {roll_dice}, you lost ${wager}.")
    
cho_han("Even", 15)
cho_han("Odds", 45)
cho_han("Even", 10)
cho_han("Odds", 25)

yea, codecademy might have restrictions. Ideally, set up your local development environment or if that is too much at the moment, you could use this website:

https://repl.it/

quotes are purely for strings, they are no way related to print. Although printing a string is common enough, you have to see them as two separate things, this is perfectly valid:

my_variable = "hello world"
print(my_variable)

see? No quotes when using print, i am just printing the variable (which holds a string)

return is literally what it says, handing back data:

def example():
   x = 3
   return x

result = example()
print(result)

so here:

result = example()

we call the function, which hands back 3 (the return value) which we store in result variable

this example might be a bit lame, but why do we do this? Because we can’t access x directly:

def example():
   x = 3
   return x

example()
print(x)

due to scope rules (local scope).

lets debug point 3 of your question.

Lets see what values the parameters receive:

def cho_han (play_choice, wager):
   print(play_choice)

that seems fine. Where else do we do something with player choice? Here:

play_choice = "Even" or "Odds"
print(play_choice)

that seems to go wrong. Why might that be?

Teaching yourself these steps to go through the code, see where things might go wrong. Its crucial to understand what each line of code does

hope this helps

1 Like

Yes! Thank you for breaking down each point. I will have to look more closely at my code as you suggested to figure out where I am going wrong.

Thanks again for taking the time to respond.

play_choice = “Even” or “Odds”
print(play_choice)

that seems to go wrong. Why might that be?
…,
On second thought… why doesn’t this work?

Is it because play_choice can only have one designated parameter (either Even or Odds) for other arguments to be passed into?

Are the quotes around Even and Odds misplaced?

Is it because print(play_choice) is not indented, or it can’t be used how I am trying to use it?

I believe I understand the broader context of certain code blocks as I continue to learn, but gaps in my understanding of certain usage types delay my progress.

Any suggestions are welcome.
Thanks in advance.

at this point i assume you can properly indent the code even though might code samples might not have the right indention level

I like the way you are thinking :slight_smile: That is actually the right mindset.

let me help you a bit:

def cho_han (play_choice, wager):
  print(play_choice)
  num1 = random.randrange(1, 7)
  num2 = random.randrange(1, 7)
  roll_dice = num1 + num2
  play_choice = "Even" or "Odds"
  print(play_choice)

if we combine the print statement, we can see that the parameter receive the right values but are then always overwritten to "Even" by this line:

  play_choice = "Even" or "Odds"

so, what does this line do exactly? How is this expression/or operator behave? Is it the way i expected it? Is there a way with a small code sample i could test this?