016
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="")
while/break 用法:arr = []
while True:
myString = list(map(lambda x: x, input("輸入字符串,輸入 stop 結束:").split()))
if myString[-1].lower() == "stop":
arr = arr + myString[:-1]
print(*arr)
break
arr = arr + myString輸入字符串,輸入 stop 結束:banana
輸入字符串,輸入 stop 結束:apple car dog cat
輸入字符串,輸入 stop 結束:flower game book stop
banana apple car dog cat flower game book拋硬幣遊戲import random
def games(n: int):
maxFlip = {"head": 0, "tail": 0}
now, count, flip = None, 0, []
for i in range(n):
res = random.choice(["head", "tail"])
flip.append(res)
if now == res:
count += 1
else:
maxFlip, count, now = max(count, maxFlip), 0, res
return (flip.count("head"), flip.count("tail"), maxFlip["head"], maxFlip["tail"])
def main():
times = int(input("輸入拋硬幣次數:\n"))
head, tail, x, y = games(times)
print(f"一共模擬了 {times} 次拋硬幣,輸出結果:\n正面:{head} 次,反面:{tail} 次\n正面最多連續次數為:{x} 次\n反面最多連續次數為:{y} 次")
if __name__ == "__main__":
main()輸入拋硬幣次數:
13
一共模擬了 13 次拋硬幣,輸出結果:
正面:6 次,反面:7 次
正面最多連續次數為:0 次
反面最多連續次數為:4 次
页:
[1]