# 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 =”.

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("Tails", 22)
``````

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():
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.

``````# 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.

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 That is actually the right mindset.

``````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?

Hello Friend,

After many hours of trying to figure out what is not working in my code, I am coming to you again for help. I know I can use “% 2” to determine “odd or even” dice outcome (reviewed other people’s code), but I can’t push myself to move forward without understanding why my code is not working.

``````import random
money = 100

# Game of Chances - Cho Han
def cho_han (play_choice, wager):
num1 = random.randrange(1, 6)
num2 = random.randrange(1, 6)
roll_dice = num1 + num2

# Setting up code for "Even"/"Odds" and roll_dice attributes
if roll_dice == [2,4,6,8,10,12] and play_choice == "Even":
print(f"The dice rolled {roll_dice}... An even!")
print(f"You won \${wager}!")
elif roll_dice == [3,5,7,9,11] and play_choice == "Odd":
print(f"The dice rolled {roll_dice}... Odds!")
print(f"You won \${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)
``````

I get the following output:

Sorry…the dice rolled 4, you lost \$15.
Sorry…the dice rolled 5, you lost \$45.
Sorry…the dice rolled 6, you lost \$10.
Sorry…the dice rolled 8, you lost \$25.

Am I not using the list (i.e., [2,4,6,8,10,12]) properly? What am I not doing right for my “if” and “elif” statements to return nothing once I call the function? What am I not doing correctly?

The code seems to always execute the else clause, indicating something is wrong with your condition.

we could temporary set the roll_dice to an even value just for testing purposes:

``````roll_dice = 8
``````

okay, now lets print the values we will use for comparison:

``````print(roll_dice, [2,4,6,8,10,12] )
if roll_dice == [2,4,6,8,10,12] and play_choice == "Even":
``````

hm… an integer equaling a list, i don’t think that is true. You want to check if role_dice is in the list

also, i would really use the modulo operator, its the more flexible solution

Fair enough. I can settle on using modulo operator. But for a deeper understanding, how exactly did you test “roll_dice = 8” ? Did you change the value in the original function and call it, or did you test it with the lines of code you suggest?

Also, can you provide an example of how to actually test code as it is written from any lines of code we have discussed?

Thanks again!

Also (not trying to bug you), but I copied a solution to Cho_Han from a Forum user pasted below. While I understand the function and lines of code, I do have question:

The “if” and “elif” conditions both return “money += bet”. Why don’t they begin with Return?

``````import random

money = 100

def cho_han(money, bet, call):
di1, di2 = random.randint(1,6), random.randint(1,6)
sum = di1 + di2
won = 'You Win'

if sum % 2==0 and call == 'Cho':
money += bet
elif sum % 2==1 and call == 'Han':
money += bet
else:
won = 'You lose'; money -= bet

print(f"The sum is: {sum}. You bet \${bet} on {call}. {won}! Total Money: \${money}.")

cho_han(money, 20, 'Han')
cho_han(money, 50, 'Cho')
cho_han(money, 70, 'Cho')
cho_han(money, 99, 'Han')
cho_han(money, 15, 'Cho')
``````

i temporary changed this line:

``````roll_dice = num1 + num2
``````

to:

``````roll_dice = 8
``````

to have consistent data instead of random data.

Of course, the better way to do this, would be to isolate the random dice function then use unittest and mock to test the behavior of your code. But i think that is a bit too complicated for now

both return money += bet? They don’t, they increase your total money amount by the amount you won.