思路:
#随机生成:3个点数的骰子
#点数计算,是大还是小
#开始游戏
- 用户本金 > 0循环游戏
- 判断用户输入是否合法
- …
import random
def roll_dict():#随机生成:3个点数的骰子
return [random.randrange(1, 7) for i in range(3)]
def roll_result(total):#点数计算,是大还是小
res = sum(total)
big = 11 <= res
small = res <= 10
if big:
return ['大','big']
else:
return ['小','small']
def start_game():
print('-----游戏开始-----')
money = 1000
while money > 0:
choice = ['大','小','big','small']
you_choice = input("选择'大'或'小'('big'或'small'):")
if you_choice in choice:
you_bet = input("下注金额:")
if you_bet.isdigit():
points = roll_dict()
result = roll_result(points)
if you_choice in result:
money += int(you_bet)
print('你赢了{}元,现有本金{}元'.format(you_bet,money))
else:
money -= int(you_bet)
print('你输了了{}元,骰子点数{},现有本金{}元'.format(you_bet,points,money))
else:
print('-----格式错误-----')
else:
print('-----格式错误-----')
else:
print('-----游戏结束-----')
start_game()