鱼C论坛

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

python新手,不知关于掷100次硬币程序是否正确,请指正

[复制链接]
发表于 2016-9-28 21:46:46 | 显示全部楼层 |阅读模式

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

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

x
# Coin

import random

head = 0
tail = 0
total = head + tail

while total < 100:
    coin = random.choice(('head', 'tail'))
    if coin == "head":
        head += 1
    else:
        tail += 1
    total = int(head + tail)
   
print("head:" ,head)
print("tail:" ,tail)

input("\n\nPress the enter key to exit.")
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-9-29 00:18:32 | 显示全部楼层
本帖最后由 jerryxjr1220 于 2016-9-29 00:21 编辑

换一种思维,用时间函数当随机函数来抓取,模拟1000次投硬币
  1. 478 522
  2. [Finished in 5.0s]
复制代码

  1. import time
  2. head, tail = 0, 0
  3. for i in range(1000):
  4.         h =int (time.time() * 10000)
  5.         time.sleep(0.002)
  6.         s = int(str(h)[-1])
  7.         if s % 2:
  8.                 head += 1
  9.         else:
  10.                 tail += 1
  11. print (head, tail)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-23 02:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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