匿名用户
9级
2012-06-18 回答
最后一个条件有问题,应该是除了7,11,2,3,12外的数字会继续摇。
#!/usr/bin/env python
#coding: utf-8
import sys
from random import seed, randint
from time import sleep
# make random seed
seed()
dashs = '-' * 50
# loop to get player chips
while True:
wallet = str(raw_input("How many chips do you want to buy? "))
if not wallet.isdigit():
print "***Error: Chips must be positive number!***"
continue
wallet = int(wallet)
break
def continue_or_not():
while True:
answer = str(raw_input("Do you want to continue (y/n) [y]?"))
answer = 'Y' if answer == '' else answer
if answer.upper() not in ('Y', 'N'):
print '***Error: Wrong answer!***'
continue
return answer.upper()
def roll_dice():
first, second = randint(1, 6), randint(1, 6)
total = first + second
print 'You rolled %d + %d = %d' % (first, second, total)
return total
# game begin
while True:
print dashs
print "There're %d chips in your wallet." % wallet
bet = str(raw_input("Please make a bet: "))
if not bet.isdigit():
print "***Error: Chips must be positive number!***"
continue
bet = int(bet)
if bet > wallet:
print "***Error: Only %d chips in your wallet!***" % wallet
continue
if bet < 1:
print "***Error: Chips must be positive number!***"
continue
total = roll_dice()
if total in (7, 11):
game_status = 'won'
elif total in (2, 3, 12):
game_status = 'lost'
else:
game_status = 'continue'
my_point = total
while game_status == 'continue':
sum = roll_dice()
if sum == my_point:
game_status = 'won'
elif sum == 7:
game_status = 'lost'
if game_status == 'won':
print 'Congratulations, you won!'
wallet += bet
else:
wallet -= bet
print 'Sorry, you lost.'
if wallet < 1:
print 'You are broken.'
break
print dashs
answer = continue_or_not()
if answer == 'Y':
continue
else:
break
# player want to quit
print dashs
print 'There are %d chips in your wallet.' % wallet
time_to_left = 5
while time_to_left > 0:
print '\\rQuit in %d seconds ...' % time_to_left,
time_to_left -= 1
sleep(1)
sys.exit(0)