 # Trouble with my coin_flip() function

I’m officially stumped,
I’ve tried everything in the book to get this code to work and I’m officially at deads end. The code skips right over my coin_flip() function and doesnt perform any of the if statements I’ve written. I know its something small but I’m officially through trying to figure this out.

``````import random

money = 100

print('\n')
print("Hello: " + name)
print("Welcome to Coin Toss Pro!")
print("Available cash winnings: " + "\$", money)
print('\n')

# Write your game of chance functions here

def coin_flip(guess, amount):
winner = random.randint(1, 2)
print("You Wagered: " + "\$", wager, " on: ", bet)
print('\n')
# conditional logic
if guess == "heads" and winner == 1:
cash_total = money + amount
#return cash_total
if guess == "heads" and winner == 1:
cash_total = money + amount
#return cash_total
if guess == "tails" and winner == 2:
cash_total = money + amount
#return cash_total
if guess == "tails" and winner == 2:
cash_total = money + amount
#return cash_total
else:
cash_total = money - amount
print("You Lost!")
print("cash total: ", cash_total)
#return cash_total
#replay = input("Play Again (y/n): ")

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

``````

You have a few issues. First you need to out-dent your function call at the end of your code.
The way you have it set up, there is no need to pass arguments to the function, nor does the function need to accept parameters. You query the user for those values inside the function itself. You would then need to change `guess` to `bet` in all of your `if` statements. Hope this helps!

2 Likes

I got this to work just a little bit ago but I’m gonna try your method out as well.

``````import random

money = 100

print('\n')
print("Hello: " + name)
print("Welcome to Coin Toss Pro!")
print("Available cash winnings: " + "\$", money)
print('\n')
int_wager = int(wager)

# Write your game of chance functions here
def coin_flip(guess, amount):
print("You Wagered: " + "\$", int_wager, " on: ", bet)
print('\n')
winner = random.randint(1, 2)
# conditional logic
if guess == "heads" and winner == 1:
cash_total = money + amount
return cash_total
if guess == "tails" and winner == 2:
cash_total = money + amount
print("Winner: Tails! " + "Your Total: ", cash_total)
return cash_total
else:
cash_total = money - amount
print("You Lost!")
print("cash total: ", cash_total)
return cash_total

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

``````

If it’s working like this, I’d leave it. I just ran your updated code, and it seems to work as expected. One suggestion you may consider is changing this line of code:

``````wager = input("Please Enter Your Wager: ")
``````

to this:

``````wager = int(input("Please Enter Your Wager: "))
``````

Then you would no longer need to convert `wager` to `int_wager`. It would be one less variable. It’s entirely up to you. Happy Coding!

@geekmonster.jt, @midlindner
Does the CA interface let you use input()? I always get `EOFError: EOF when reading a line` when I do so, at least in this particular project.

Trying it out now

Thanks!!!

Had the same problem so I just did mine in PyCharm. Must be a reason why they wanted Anaconda in the mix.

1 Like

I haven’t started any of the CC Python courses, but I have noticed in other courses that not all methods are available at every level, so I’m not surprised that’s the case.