10 月 152020
import random #引用
#定义函数
def roll_dice(number=3, points=None): #指定参数默认值
print('>>>>> ROLL THE DiCE! <<<<<')
if points is None: #判断,如果point为空,默认赋值为空
points = [] #空列表赋值变量
while number > 0: #循环,number大于0则执行
point = random.randrange(1,7) #生成随机数赋值变量
points.append(point) #将变量写入列表
number = number - 1 #每次将number减1,循环3次,为0时退出循环
return points #调用函数,返回已赋值变量(3个随机数)
#定义函数
def roll_result(total):
isBig = 11 <= total <= 18 #11-18之间的值赋值变量
isSmall = 3 <= total <=10 #3-10之间的值赋值变量
if isBig: #判断,返回大或小
return 'Big'
elif isSmall:
return 'Small'
#定义函数
def start_game():
print('>>>>> STARTS! <<<<<')
choices = ['Big', 'Small'] #列表赋值变量
your_choice = input('Big or Small: ') #用户输入赋值变量
you_have = 1000 #初始金额
you_bet = input('How much you wanna bet? ') #输入下注金额
if your_choice in choices: #判断用户输入是否存在于列表中
points = roll_dice() #调用函数获得随机数并赋值变量
total = sum(points) #计算随机数的和
youWin = your_choice == roll_result(total) #用户输入与函数计算结果比较返回True并赋值变量
if youWin: #if True 打印成功
print('The points are ', points, 'You win!')
#重新计算持有金额并打印金额信息
you_have = you_have + int(you_bet)
print('You gained ', you_bet,', you have ', you_have, 'now')
else: #反之为False,打印失败
print('The points are ', points, 'You lose!')
#重新计算持有金额并打印金额信息
you_have = you_have - int(you_bet)
#判断,持有金额大于0的情况和小于0的情况
if you_have > 0: #打印信息并继续
print('you lose ', you_bet,', you have ', you_have,' now')
else: #反之,游戏结束
print('you lose ', you_bet,', you have ', you_have,' now')
print('GAME OVER')
exit(0)
else: #其他情况(用户输入与列表中元素不一致),提示用户输入无效字符
print('Ivalid Words')
start_game() #再次调用函数
#调用函数,执行
start_game()