1 Star 0 Fork 0

全宗/exercise-of-python3-2022

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
wfl.v1.py 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
"""
中午吃啥?
锤子在手见啥都是钉子。
"""
import random
import time
choice = random.choice(range(1,81))
if choice >=80:
print("泡面")
elif choice>=60:
print("沙县")
elif choice>=40:
print("快餐")
elif choice>=20:
print("炒菜")
elif choice>=0:
print("烧味")
"""
这段代码有问题。可以用以下代码验证:
"""
start = time.perf_counter()
count = range(1000000)
n1 = 0
n2 = 0
n3 = 0
n4 = 0
n5 = 0
for i in count:
choice = random.choice(range(1,81))
if choice >=80:
n1 += 1
elif choice>=60:
n2 += 1
elif choice>=40:
n3 += 1
elif choice>=20:
n4 += 1
elif choice>=0:
n5 += 1
print("泡面次数:",n1)
print("沙县次数:",n2)
print("快餐次数:",n3)
print("炒菜次数:",n4)
print("烧味次数:",n5)
end = time.perf_counter()
print("运行时间为", round(end-start), 'seconds')
"""
运行时间比v2版本的长,而且各项出现次数不平均,尤其是泡面。
测试1,000,000次:
泡面次数: 12535
沙县次数: 250330
快餐次数: 250197
炒菜次数: 248915
烧味次数: 238023
运行时间为 2 seconds
"""
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/quan-zong/exercise-of-python3-2022.git
git@gitee.com:quan-zong/exercise-of-python3-2022.git
quan-zong
exercise-of-python3-2022
exercise-of-python3-2022
master

搜索帮助