鱼C论坛

 找回密码
 立即注册
查看: 1918|回复: 1

[技术交流] 016

[复制链接]
发表于 2021-8-31 22:20:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
0.
while循环,中断和被打败等价于break
1.
少一个=
2.
45
3.

4.
ctrl+c
5.
B,逻辑通畅

----------------------
0.
while True:
    slogan = input("please input a slogan(end with stop):")
    if slogan == "stop":
        break
    print(slogan)

1.


2.
import random

counts = int(input("请输入抛硬币的次数:"))
i = 0
front = 0
back = 0
tfront = 0
tback = 0
ffront = 0
fback = 0

print("开始抛硬币实验:")
while i < counts:
    num = random.randint(1, 10)
    if counts < 100:
        if num % 2:
            print("正面", end=" ")
            front += 1
            flag = 0
        else:
            print("反面", end=" ")
            back += 1
            flag = 1
        if flag == 0:
            tfront += 1
            tback = 0
            if ffront < tfront:
                ffront = tfront
        else:
            tback += 1
            tfront = 0
            if fback < tback:
                fback = tback
        i += 1
    else:
        if num % 2:
            front += 1
            flag = 0
        else:
            back += 1
            flag = 1
        if flag == 0:
            tfront += 1
            tback = 0
            if ffront < tfront:
                ffront = tfront
        else:
            tback += 1
            tfront = 0
            if fback < tback:
                fback = tback
        i += 1
print("the number of simulation is ",front+back,"times,the result is as follow:")
print("front:",front)
print("back:",back)
print("the most time of front is:",ffront)
print("the most time of back is:",fback)


**************************************
1.小甲鱼写的更优雅
import random

counts = int(input("请输入抛硬币的次数:"))

# 利用 ignore 变量来判断是否打印每次的结果
if counts > 100:
    ignore = True
else:
    ignore = False

heads = 0 # 统计正面的次数
tails = 0 # 统计反面的次数

i = 0
print("开始抛硬币实验……")
while i < counts:
    num = random.randint(1, 10)

    if num % 2:
        heads += 1
        if not ignore:      
            print("正面", end=" ")
    else:
        tails += 1
        if not ignore:
            print("反面", end=" ")

    i += 1

print("")
print("一共模拟了", counts, "次抛硬币,结果如下:")
print("正面:", heads, "次", sep="")
print("反面:", tails, "次", sep="")


2.

import random

counts = int(input("请输入抛硬币的次数:"))

# 利用 ignore 变量来判断是否打印每次的结果
if counts > 100:
    ignore = True
else:
    ignore = False

heads = 0 # 统计正面的次数
tails = 0 # 统计反面的次数

last = 0 # 记录上一次的状态,如果是正面设置为1, 反面则设置为2
c_heads = 0 # 统计连续正面的次数
c_tails = 0 # 统计连续反面的次数
max_heads = 0 # 统计连续正面的最多次数
max_tails = 0 # 统计连续反面的最多次数

i = 0
print("开始抛硬币实验……")
while i < counts:
    num = random.randint(1, 10)

    if num % 2:
        heads += 1
        c_heads += 1

        if not ignore:      
            print("正面", end=" ")

        # 如果上一次是反面:将连续正面的次数设置为1
        if last == 2:
            c_heads = 1

        # 判断连续正面的次数是否比max_heads大,如果是,取而代之
        if c_heads > max_heads:
            max_heads = c_heads

        # 将上一次的状态设置为正面
        last = 1
    else:
        tails += 1
        c_tails += 1
        
        if not ignore:
            print("反面", end=" ")

        # 如果上一次是正面:连续反面的次数设置为1
        if last == 1:
            c_tails = 1

        # 判断连续反面的次数是否比max_tails大,如果是,取而代之
        if c_tails > max_tails:
            max_tails = c_tails

        # 将上一次的状态设置为反面
        last = 2
        
    i += 1

print("")
print("一共模拟了", counts, "次抛硬币,结果如下:")
print("正面:", heads, "次", sep="")
print("反面:", tails, "次", sep="")
print("最多连续正面:", max_heads, "次", sep="")
print("最多连续反面:", max_tails, "次", sep="")


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-8 12:29:42 | 显示全部楼层
while/break 用法:
  1. arr = []
  2. while True:
  3.     myString = list(map(lambda x: x, input("輸入字符串,輸入 stop 結束:").split()))
  4.     if myString[-1].lower() == "stop":
  5.         arr = arr + myString[:-1]
  6.         print(*arr)
  7.         break
  8.     arr = arr + myString
复制代码
  1. 輸入字符串,輸入 stop 結束:banana
  2. 輸入字符串,輸入 stop 結束:apple car dog cat
  3. 輸入字符串,輸入 stop 結束:flower game book stop
  4. banana apple car dog cat flower game book
复制代码
拋硬幣遊戲
  1. import random

  2. def games(n: int):
  3.     maxFlip = {"head": 0, "tail": 0}
  4.     now, count, flip = None, 0, []
  5.     for i in range(n):
  6.         res = random.choice(["head", "tail"])
  7.         flip.append(res)
  8.         if now == res:
  9.             count += 1
  10.         else:
  11.             maxFlip[res], count, now = max(count, maxFlip[res]), 0, res
  12.     return (flip.count("head"), flip.count("tail"), maxFlip["head"], maxFlip["tail"])

  13. def main():
  14.     times = int(input("輸入拋硬幣次數:\n"))
  15.     head, tail, x, y = games(times)
  16.     print(f"一共模擬了 {times} 次拋硬幣,輸出結果:\n正面:{head} 次,反面:{tail} 次\n正面最多連續次數為:{x} 次\n反面最多連續次數為:{y} 次")

  17. if __name__ == "__main__":
  18.     main()
复制代码
  1. 輸入拋硬幣次數:
  2. 13
  3. 一共模擬了 13 次拋硬幣,輸出結果:   
  4. 正面:6 次,反面:7 次
  5. 正面最多連續次數為:0 次
  6. 反面最多連續次數為:4 次
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 22:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表