如何解决尝试模拟 2 个骰子以了解相同结果的百分比
所以,我认为它们是代码中某处的逻辑错误。 无论我在输出终端中插入任何数字,输出都会保持运行 0%
#dice - count doubles in user - defined number of rounds.. repeated
import random
#simulate rolling a six sided die and return its value.
def rollOneDie():
#generate random numbers from 1 to 6
thisFace = random.randrange(1,7)
return thisFace
while True:
nDoubles = 0
maxRounds = input(
"how many rounds do you want to roll?(or ENTER to quit):")
if maxRounds == '':
break
try:
maxRounds = int(maxRounds)
except:
print("please an integer number")
continue
for roundNumbers in range(0,maxRounds):
die1 = rollOneDie()
die2 = rollOneDie()
if die1 == die2:
nDoubles = nDoubles + 1
percent = (nDoubles * 100.0) / maxRounds
print('out of ',maxRounds,'you rolled',nDoubles,'doubles or ',percent,'%')
print('ok byeee')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。