我知道有类似问题,但请耐心等待。我正在帮助我的小表弟在python中创建一个程序,其中一个硬币被翻转1000次并且“玩家”可以说是猜测了多少时间头部出现。然后,她想检查猜测是否在头部出现的次数的10倍之内并且相应地做出反应(骗子你(回答 - 猜测)关闭)或(只做好工作(回答 - 猜测)关闭)。我习惯使用UNIX和python似乎以一种非常不同的方式进行项目。这是代码可能以某种方式使用范围来检查猜测是否在范围内?
#Guess how many times heads will occur when a coin is flipped 1000 time
import random
import time
print('I will flip a coin 1000 times. Guess how many times it will come up heads. (Press enter to begin)')
int(guess) = input()
flips = 0
heads = 0
while flips < 1000:
if random.randint(0, 1) == 1:
heads = heads + 1
flips = flips + 1
if flips == 900:
print('900 flips and there have been ' + str(heads) + ' heads.')
time.sleep(5)
if flips == 100:
print('At 100 tosses, heads has come up ' + str(heads) + ' times so far.')
time.sleep(2)
if flips == 500:
print('Half way done, and heads has come up ' + str(heads) + ' times.')
time.sleep(2)
print()
print('Out of 1000 coin tosses, heads came up ' + str(heads) + ' times!')
time.sleep(2)
print('Were you close?')
int(answer) = input()
答案 0 :(得分:6)
if heads - 10 <= int(guess) <= heads + 10:
替代地
if abs(heads - int(guess)) <= 10: