10 月 212020
# -*- coding:utf-8 -*-
import random # 导入内置模块,用以产生随机数【pycharm:行内注释,#号前需空两个字符,#号后需空一个字符】
# 定义摇色子函数,次数参数默认为3,点数参数默认为空
def roll_dice(numbers=3, points=None): # 【PEP 8: E251 unexpected spaces around keyword / parameter equals】
print('>>>>> ROLL THE DICE! <<<<<')
if points is None: # 判断,如果点数参数值为空,将空列表赋值给该变量
points = []
while numbers > 0: # while循环,直到次数值不在大于0时终止,初始值为3,每循环一次减1
point = random.randrange(1, 7) # 在1-6范围内取随机数赋值给点数
points.append(point) # 将点数追加写入列表
numbers = numbers - 1 # 最多循环3次,成功获取3个随机数,跳出循环
return points # 返回生成的3个随机数
def roll_result(total): # 定义比较大小函数
big = 11 <= total <= 17 # 比较运算的布尔值True赋值给变量【pycharm:变量应当是小写】
small = 4 <= total <= 10 # 比较运算的布尔值True赋值给变量
if big: # 判断,如果big == True 则返回提示Big
return 'Big'
elif small: # 继续判断,如果small == True 则返回Small提示
return 'Small'
def start_game(): # 定义启动游戏函数
your_money = 1000 # 初始持有金额并赋值变量【PEP 8: E225 missing whitespace around operator】
while your_money > 0: # 循环,游戏重复开始,直到持有金额不再大于0时终止
print('>>>>> GAME STARTS! <<<<<')
choices = ['Big', 'Small'] # 定义用户有效输入值列表并赋值变量
your_choice = input('Big or Small: ') # 提示用户输入并赋值变量
if your_choice in choices: # 执行判断,是否用户输入值,存在于列表中,匹配则继续执行
your_bet = int(input('How much you wanna bet? ')) # 用户输入下注金额赋值变量
points = roll_dice() # 调用函数生成随机数
total = sum(points) # 调用函数计算随机数累加值并赋值变量
win = your_choice == roll_result(total) # 调用roll_result(total)函数取得实际大小值,与用户输入值布尔运算,取得布尔值赋值变量
if win: # 判断,win == True则执行
print('The points is ', points, ' You Win') # 打印点数
print('You gained {}, you have {} now'.format(your_bet, your_money + your_bet)) # 打印下注值和当前持有值
your_money = your_money + your_bet # 更新当前持有值
else: # 判断,反之,youWin == False则执行
print('The points is ', points, ' You Lose')
print('You lose {}, you have {} now'.format(your_bet, your_money - your_bet))
your_money = your_money - your_bet
else: # 反之,提示输入无效字符
print('Invalid Words')
else: # 反之,持有金额小于等于0时,提示游戏结束,退出执行
print('GAME OVER')
start_game() # 调用函数启动游戏【PEP 8: E305 expected 2 blank lines after class or function definition, found 1】