Skip to content

Instantly share code, notes, and snippets.

@sammolk
Created July 25, 2021 08:16
Show Gist options
  • Save sammolk/a2f1f5505fe44d4c5fd3f512281ed1c9 to your computer and use it in GitHub Desktop.
Save sammolk/a2f1f5505fe44d4c5fd3f512281ed1c9 to your computer and use it in GitHub Desktop.
lucky unicorn fully working program, combining all components first version of fully working program, not yet tested for usability
# lucky unicorn fully working program, combining all components
# first version of fully working program, not yet tested for usability
import random
# function goes here
def int_check(question, low, high):
valid = False
while not valid:
error = "Whoops! please enter a number between {}" \
" and {}".format(low, high)
try:
response = int(input(question.format(low, high)))
if low <= response <= high:
return response
else:
print(error)
print()
except ValueError:
print(error)
# main routine goes here
HOW_MUCH = 100 # constant representing the amount spent by user
ZEBRA_HORSE_PAYOUT = 0.5
UNICORN_PAYOUT = 5
GAME_FEE = 1
MAX_AMOUNT = 10
# ask how much the user wants to play with
user_balance = int_check("How much money would you like"
" to play with?", GAME_FEE, MAX_AMOUNT)
keep_playing = ""
while not keep_playing:
# balancing the odds - now only 10% chance to get unicorn
tokens = ["donkey", "donkey", "donkey", "horse", "horse", "horse",
"zebra", "zebra", "zebra", "unicorn"]
# generate random choice taken off list
token = random.choice(tokens)
print("\nyou got a {}".format(token))
# adjust user_balance correctly for given token
if token == "donkey":
user_balance -= 1
print("sorry, you didn't win anything this round")
elif token == "zebra" or token == "horse":
user_balance += ZEBRA_HORSE_PAYOUT - 1
print("sorry, you only won ${:.2f}".format(ZEBRA_HORSE_PAYOUT))
elif token == "unicorn":
user_balance += UNICORN_PAYOUT - 1
print("congratulations you won ${:.2f}".format(UNICORN_PAYOUT))
# check and report user_balance, giving option to play again if at least $1
if user_balance >= GAME_FEE:
print("you have ${:.2f} left to play with".format(user_balance))
keep_playing = input("press <enter> to play another round or"
" press 'Q' to quit\n")
else:
print("you don't have enough credit for another round")
keep_playing = "Q"
# farewells the player at the end of the game
print("\nyou are leaving with a balance of ${:.2f}"
"\nthanks for playing. goodbye".format(user_balance))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment