Last active
August 29, 2015 14:06
-
-
Save danhyun/1ea9b4713a0bb295ccc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """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 sys, random | |
| start_money = 100 | |
| human_money = start_money | |
| computer_money = start_money | |
| minimum_bet = 1 | |
| maximum_bet = 10 | |
| round_win_message = "You win this round!" | |
| round_lose_message = "Your opponent wins this round." | |
| prompt = "Even or Odd? " | |
| def is_even(i): | |
| return i & 1 == 0 | |
| def is_odd(i): | |
| return i & 1 == 1 | |
| def has_lost(i): | |
| return i < maximum_bet * 2 | |
| def get_user_bet(): | |
| #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!" | |
| sys.exit(0) | |
| if not maximum_bet >= user_bet >= minimum_bet: | |
| print "You have entered an invalid entry. Maximum bet is %s and minimum bet is %s." | |
| % (maximum_bet, minimum_bet) | |
| continue | |
| break | |
| except ValueError: | |
| print "Please enter a valid number." | |
| if user_bet == 0: | |
| sys.exit(0) | |
| #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(prompt) | |
| while input.lower() not in ['even', 'odd']: | |
| print "You have entered an invalid entry." | |
| input = raw_input(prompt) | |
| input = input.lower() | |
| 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) | |
| user_bet = get_user_bet() | |
| 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(minimum_bet, maximum_bet) | |
| print "Your opponent bet %s." % computer_bet | |
| number = computer_bet + user_bet | |
| print "The combined bet is %s." % number | |
| #This is where we determine whether the User wins or Computer wins. | |
| human_wins_round = (input == 'even' and is_even(number)) or (input == 'odd' and is_odd(number)) | |
| if human_wins_round: | |
| print round_win_message | |
| human_money += number | |
| computer_money -= number | |
| else: | |
| print round_lose_message | |
| human_money -= number | |
| computer_money += number | |
| #This is where we make sure that each player has enough funds to continue playing the game. | |
| if has_lost(human_money): | |
| print "You have insufficient funds to continue." | |
| print "Thanks for playing! Goodbye!" | |
| sys.exit(0) | |
| if has_lost(computer_money): | |
| print "Your opponent has insufficient funds to continue." | |
| print "You win! Thanks for playing! Goodbye!" | |
| sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment