Sample solution for Assignment 1
Click here to get the file
Size
1 kB
-
File type
text/python-source
File contents
import random
bank = 10
while(bank > 0):
bet = input('You have $%d, how much would you like to bet? ' % (bank))
if bet == 0:
print "Congratulations! You have chosen to quit with $%d in the bank." % (bank)
break
elif bet > bank:
print "You don't have that much!"
continue
elif bet < 1:
print "Please enter a positive amount."
continue
choice = input('What number would you like to bet on? ')
while choice < 1 or choice > 6:
print "Please enter a number between 1 and 6."
choice = input('What number would you like to bet on? ')
die = [0,0,0]
for i in range(3):
die[i] = random.randint(1,6)
matches = die.count(choice)
if matches > 0:
payoff = bet*matches
print 'The numbers rolled are %d, %d, and %d, you win $%d.' % (die[0], die[1], die[2], payoff)
bank = bank + payoff
else:
print 'The numbers rolled are %d, %d, and %d, you lose $%d.' % (die[0], die[1], die[2], bet)
bank = bank - bet
if bank == 0:
print("I am sorry, but your bank balance is zero dollars. Good bye!")
by
Max Chang
—
last modified
2007-01-26 09:12 AM