Skip to content

Instantly share code, notes, and snippets.

@JamesJinPark
Last active August 29, 2015 14:05
Show Gist options
  • Save JamesJinPark/f0645afde3ad18e769d7 to your computer and use it in GitHub Desktop.
Save JamesJinPark/f0645afde3ad18e769d7 to your computer and use it in GitHub Desktop.
Assignment 1
"""I am the creator of this program.
This is a gambling game where one player is "Even" and the other player is "Odd."
Each player begins with some money, and each player chooses some money to bet.
If the sum is odd, the odd player wins.
If the sum is even, the even player wins.
Play ends when a player has less than twice the maximum bet.
Play also ends when the human player decides to quit by entering a bet of 0.
"""
import random
start_money = 100
human_money = start_money
computer_money = start_money
maximum_bet = 10
minimum_bet = 1
#This is where the User chooses whether to play Even or Odd.
raw_input("This is a gambling game where you choose to be Even or Odd. Press enter to play. ")
input = raw_input("Even or Odd? ")
while input.lower() not in ['even', 'odd']:
print "You have entered an invalid entry."
input = raw_input("Even or Odd? ")
print "You chose %s." % input
#This is where the program loops so that the User can continue to play after the first game.
while True:
total_pool = (human_money, computer_money)
raw_input("You have %s to bet. Your opponent has %s. Press enter to continue. " % total_pool)
#This is where the User bets.
while True:
try:
user_bet = int(raw_input("How much do you want to bet? "))
if user_bet == 0:
print "Thanks for playing! Goodbye!"
break
if not maximum_bet >= user_bet >= minimum_bet:
print "You have entered an invalid entry. Maximum bet is 10 and minimum bet is 1."
continue
break
except ValueError:
print "Please enter a valid number."
if user_bet == 0:
break
print "Your bet is %s." % user_bet
#This is where we set up how the add the User's bet with the Computer's random bet.
computer_bet = random.randint (1, 10)
print "Your opponent bet %s." % computer_bet
number = int(computer_bet + user_bet)
print "The combined bet is %s." % number
#This is where we determine whether the User wins or Computer wins.
def add_human_wins (number, human_money):
return number + human_money
def add_computer_wins (number, computer_money):
return number + computer_money
def subtract_computer_loses (computer_money, number):
return computer_money - number
def subtract_human_loses (human_money, number):
return human_money - number
if input.lower() == 'even':
if (number % 2 == 0):
print "You win this round!"
subtract_human_loses(human_money, number)
subtract_computer_loses(computer_money, number)
add_human_wins(number, human_money)
human_money = add_human_wins (number, human_money)
computer_money = subtract_computer_loses(computer_money, number)
else:
print "Your opponent wins this round."
subtract_human_loses(human_money, number)
subtract_computer_loses(computer_money, number)
add_computer_wins(number, computer_money)
computer_money = add_computer_wins(number, computer_money)
human_money = subtract_human_loses(human_money, number)
else:
if (number % 2 == 0):
print "Your opponent wins this round."
subtract_human_loses(human_money, number)
subtract_computer_loses(computer_money, number)
add_computer_wins(number, computer_money)
computer_money = add_computer_wins(number, computer_money)
human_money = subtract_human_loses(human_money, number)
else:
print "You win this round!"
subtract_human_loses(human_money, number)
subtract_computer_loses(computer_money, number)
add_human_wins(number, human_money)
human_money = add_human_wins(number, human_money)
computer_money = subtract_computer_loses(computer_money, number)
total_pool = (human_money, computer_money)
#This is where we make sure that each player has enough funds to continue playing the game.
if human_money < maximum_bet * 2:
print "You have insufficient funds to continue."
print "Thanks for playing! Goodbye!"
break
if computer_money < maximum_bet * 2:
print "Your opponent has insufficient funds to continue."
print "You win! Thanks for playing! Goodbye!"
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment