【问题标题】:When using this loop, I'm not getting the results I want and I'm not sure how to get the code to work on Python使用这个循环时,我没有得到我想要的结果,我不确定如何让代码在 Python 上运行
【发布时间】:2014-04-26 06:54:12
【问题描述】:

我正在尝试制作正面和反面结果表,每次“硬币”从 1-10、1-100、1-1,000、1-10,000 和 1- 的范围内“翻转” 100,000。但我第一次折腾后的结果喜忧参半。

import random

print("heads  tails")
resultList=[0,0]
start=10
maximum=[]

for exponent in range (1,6):
    ranges=start**exponent   
     for num in range(1,ranges+1):
        result=random.randint(0,num)
        if result%2==0:
            resultList[0]+=1
        elif result%2 != 0:
            resultList[1]+=1
    print(resultList)

但我的输出在第一次折腾时表现良好,然后一切都走下坡路。

heads  tails
[3, 7]
[49, 61]
[566, 544]
[5533, 5577]
[55165, 55945]

关于我做错了什么有什么建议吗?有些东西告诉我,我忽略了一些简单的东西。

【问题讨论】:

  • 顺便说一下,错误是结果的第一行应该输出 2 个数字,加起来是 10,然后是 100,然后是 1,000,依此类推,直到 100,000。

标签: python output


【解决方案1】:

在上一次迭代之后,您没有清除 resultList。只需将 resultList=[0,0] 移动到 for 循环内即可。

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多