1 import random 2 point = random.randrange(1,7) 3 # random.randrange(1,7)生成1-6的随机数 4 print(point)
import random
def roll_dice(numbers = 3,points = None):
print('----- 摇骰子 -----')
if points is None:
points = []
# points为空列表,后续可以插入新值到该列表
while numbers > 0:
point = random.randrange(1,7)
points.append(point)
# 用append()方法将point数值插入points列表中
numbers = numbers - 1
# 完成一次,numbers减1,当小于等于0时不再执行该循环
return points
def roll_result(total): isBig = 11 <= total <=18 isSmall = 3 <= total <= 10 if isBig: return '大' elif isSmall: return '小'
def start_game():
your_money = 1000
while your_money > 0:
print('----- 游戏开始 -----')
choices = ['大','小']
# choices赋值为大和小,用户需输入二者之一为正确
your_choice = input('请下注,大 or 小:')
your_bet = input('下注金额:')
if your_choice in choices:
points = roll_dice()
# 调用roll_dice函数
total = sum(points)
# sum为相加,将3个骰子的结果相加
youWin = your_choice == roll_result(total)
if youWin:
print('骰子点数:',points)
print('恭喜,你赢了 {} 元,你现在有 {} 元本金'.format(your_bet,your_money + int(your_bet)))
# your_bet是字符串格式,这里需要转化为int类型进行计算
your_money = your_money + int(your_bet)
# 最新本金
else:
print('骰子点数:',points)
print('很遗憾,你输了 {} 元,你现在有 {} 元本金'.format(your_bet, your_money - int(your_bet)))
your_money = your_money - int(your_bet)
else:
print('格式有误,请重新输入')
# 如果输入的不是choices列表中的大或小,则为格式有误
else:
print('游戏结束')
start_game()
import random
def roll_dice(numbers = 3,points = None):
print('----- 摇骰子 -----')
if points is None:
points = []
while numbers > 0:
point = random.randrange(1,7)
points.append(point)
numbers = numbers - 1
return points
def roll_result(total):
isBig = 11 <= total <=18
isSmall = 3 <= total <= 10
if isBig:
return '大'
elif isSmall:
return '小'
def start_game():
your_money = 1000
while your_money > 0:
print('----- 游戏开始 -----')
choices = ['大','小']
your_choice = input('请下注,大 or 小:')
your_bet = input('下注金额:')
if your_choice in choices:
points = roll_dice()
total = sum(points)
youWin = your_choice == roll_result(total)
if youWin:
print('骰子点数:',points)
print('恭喜,你赢了 {} 元,你现在有 {} 元本金'.format(your_bet,your_money + int(your_bet)))
your_money = your_money + int(your_bet)
else:
print('骰子点数:',points)
print('很遗憾,你输了 {} 元,你现在有 {} 元本金'.format(your_bet, your_money - int(your_bet)))
your_money = your_money - int(your_bet)
else:
print('格式有误,请重新输入')
else:
print('游戏结束')
start_game()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有