I’m trying to complete the game of chance Python project, but I’m getting stuck around task 8.
The task is as follows:
Expand your program to check for edge cases. What should happen if a player tries to bet more money than they have? What should happen if a player bets a negative amount of money? What should happen if a player calls "heads"
or "Heads!"
rather than "Heads"
.
Try to make it very difficult for someone to break your program.
I can’t figure out how to make the games of chance return an error if the player tries to be more than they have, or a negative amount.
I tried implementing an if statement in the first game of chance that looked like this;
if bet <= 0:
return: “You can’t bet nothing”
elif bet > money:
return “You can’t bet more than you have!.”
but I get the following error:
Traceback (most recent call last):
File “script.py”, line 73, in
money += coin_flip(110, 2)
TypeError: unsupported operand type(s) for +=: ‘int’ and ‘str’
Please help. I have put my code below. Everything worked perfectly until I got to step 8.
import random
money = 100
#Write your game of chance functions here
def coin_flip(bet, coin_side):
heads = 1
tails = 2
num = random.randint(1,2)
if num == coin_side:
return (bet * 2)
else:
return (0 - bet)
def even_num(dice):
if (dice%2) == 0:
return even
def odd_num(dice):
if (dice%2) != 0:
return odd
even = even_num
odd = odd_num
def cho_han(guess, bet):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
result = dice1 + dice2
if (guess == even) and (result % 2 == 0):
return (bet * 2)
elif (guess == odd) and (result % 2 != 0):
return (bet * 2)
elif (guess == even) and (result % 2 != 0):
return (0 - bet)
else:
return (0 - bet)
def card_pick(bet):
player1 = random.randint(1,13)
player2 = random.randint(1,13)
if player1 > player2:
return (bet*5)
elif player2 > player1:
return (0 - bet)
else:
return (0 - bet)
def roulette(guess, bet):
result = random.randint(0,36)
if (guess == even) and (result % 2 == 0):
return bet*2
elif (guess == odd) and not (result % 2 == 0):
return bet*2
elif (guess == even) and (result % 2 != 0):
return (0 - bet)
elif (guess== odd) and (result % 2 == 0):
return (0 - bet)
elif (guess == int) and (result == guess):
return bet*36
elif (guess == int) and (result != guess):
return (0 - bet)
else:
return (0 - bet)
#Call your game of chance functions here
money += coin_flip(220, 2)
print("You have " + str(money) + " after the coin flip.")
money += cho_han(even, 100)
print("You have " + str(money) + " after cho han.")
money += card_pick (60)
print("You have " + str(money) + " after picking a card.")
money += roulette(even, 50)
print("You have " + str(money) + " after playing roulette.")