# 06 Day

06 Day

1.CRAPS赌博游戏

CRAPS赌博游戏:
玩家摇两颗色子,如果第一次摇出了7点或11点,玩家胜;
如果摇出了2点、3点或12点,庄家胜;
其他情况不分胜负,游戏继续。
玩家重新摇色子,如果摇出了第一次摇的点数,玩家胜;
如果摇出了7点,庄家胜

其他情况游戏继续,直到分出胜负。

import random

money = 1000
while money > 0:

    print(f'玩家总资产:{money}元')
    debt = int(input('请下注:'))
    while debt <= 0 or debt >=money:
        debt = int(input('请下注'))
    first_point = random.randint(1, 6) + random.randint(1, 6)
    print(f'玩家摇出了{first_point}点')
    if first_point in [7, 11]:
        print('玩家胜')
        money += debt
    elif first_point in [2, 3, 12]:
        print('庄家胜')
        money -= debt
    else:
        game_over = False
        while not game_over:
            current_point = random.randint(1, 6) + random.randint(1, 6)
            print(f'玩家摇出了{current_point}点')
            if current_point == first_point:
                print('玩家胜')
                money += debt
                game_over = True
            elif current_point == 7:
                print('庄家胜')
                money -= debt
                game_over = True
print('玩家破产,游戏结束!')

2.快捷键

ctr+d 复制一行代码

ctr+y 删除一行代码

code - > reformat code 重新格式化整个文件

敲两下ctr + 空格 ————> 万能提示

debug 在代码行数前放断点,debug可以逐行执行代码,进行调试

列表.sort----->给列表排序(默认升序),如果加上(reverse = True)则为降序

当遇到不会的函数时,单击shift+f1则可以看到官方参数说明

代码文件右击local history---->show history 可以查看历史记录的代码

3.列表的复习

1.列表的重复运算

a = [0] * 10     # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

练习:判断掷骰子6000次各个点数出现的次数。
import random
fs = [0] * 6
for _ in range(6000):
    face = random.randint(1, 6)
    # 获取列表元素的索引运算
    fs[face - 1] += 1
for i in range(len(fs)):
    print(f'{i}点出现了{fs[i-1]}次')

2.列表的生成式(推导式) ——>创建列表的一种字面量语法

# 生成含有1-100的列表
a = [for x in range(1,101)]
print(a)
b = [x for x in range(1, 101) if x % 3== 0 or x % 5 == 0]
print(b)

d = [x ** 2 for x in range(1, 11)]
print(d)

c=[x for x in range(2,101,2)]
print(c)

练习:模拟双色球抽奖过程,红球1-33号抽六个,篮球1-16号抽一个(不能重复,大小排序)

import random
n = int(input('机选几注:'))
for _ in range(n):
    red1 = [x for x in range(1, 34)]
    # sc = []
    # for _ in range(6):
    #     index = random.randint(0, len(red1)-1)
    #     sc.append(red1.pop(index))
    sc = random.sample(red1, 6)
    #               抽样总体 , 样本数量
    # 通过random模块的sample函数实现无放回随机抽样
    sc.sort()
    # 给元素排序从小到大
    bule = random.randint(1, 16)
    sc.append(bule)
    for ball in sc:
        # 格式化输出整数,不够两位左边补零
        print(f'{ball:0>2d}', end=' ')
    print()

练习:约瑟夫环问题

有15个男人和15个女人乘船在海上遇险,为了让一部分人活下来,
不得不将其中15个人扔到海里,有个人想了个办法让大家围成一个圈,
由某个人开始从1报数,报到9的人就扔到海里面,他后面的人接着从1开始报数,
报到9的人继续扔到海里面,直到将15个人扔到海里。
最后15个女人都幸免于难,15个男人都被扔到了海里。
问这些人最开始是怎么站的,哪些位置是男人,哪些位置是女人。

persons = [True]*30
index, counter, num = 0, 0, 0    # 元组
#  下标,计数,数字
while counter < 15:
    if persons[index]:
        num += 1
        if num == 9:
            persons[index] = False
            counter += 1
            num = 0
    index += 1
    if index == 30:
        index = 0
#    或者 index %= 30(确保index的值保存在0-30之间)
for person in persons:
    print('女'if person else '男', end=' ')  # end=' '表示在结果后面空一格
#     三元条件运算,if后面的条件如果成立,取女
#                if后面的条件如果不成立,取else后面的男
print()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值