2

我的代码进退两难,首先我必须编写一个练习来实现以下目标

锻炼

创建一个文件以完成此练习。编写一个程序,生成 100 个随机数(范围为 1-1000),并计算其中有多少是偶数,有多少是奇数。将结果显示在屏幕上,如下面的示例输出所示。提示:使用 while 循环循环 100 次。

我的结果:

import random
num = 0
odd = 0
even = 0

while num < 100:
    random.randint(1,1000)
    num = num + 1
    #print(num)
    if random.randint(1,1000)%2==0:
        even = even + 1
    else:
        odd = odd + 1

print ("Out of 100 Random Numbers,",even,"were even and",odd,"were Odd")

输出:

Out of 100 Random Numbers, 50 were even and 50 were Odd

所有肉汁!

下一个活动:

添加另一个 while 循环,该循环将部分 a 重复 10 次。将结果显示在屏幕上,如下面的示例输出所示。

输出如下:

Out of 100 random numbers, 56 were odd, and 44 were even.
Out of 100 random numbers, 60 were odd, and 40 were even.
Out of 100 random numbers, 47 were odd, and 53 were even.
Out of 100 random numbers, 54 were odd, and 46 were even.
Out of 100 random numbers, 48 were odd, and 52 were even.
Out of 100 random numbers, 53 were odd, and 47 were even.
Out of 100 random numbers, 46 were odd, and 54 were even.
Out of 100 random numbers, 52 were odd, and 48 were even.
Out of 100 random numbers, 53 were odd, and 47 were even.

所以我写了:

import random
num = 0
odd = 0
even = 0
loop = 0


while loop < 10:
    loop = loop +1

    while num < 100:
        num = num + 1
        rand = random.randint(1,1000)
        #print(num)
        if rand%2==0:
            even = even + 1
        else:
            odd = odd + 1
    result = print ("Out of 100 Random Numbers,",even,"were even and",odd,"were Odd")

导致输出:

Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd
Out of 100 Random Numbers, 50 were even and 50 were Odd

你们中的一位优秀程序员能否向我解释为什么我会得到这个结果和/或修改代码以获得练习的预期结果。

解释为什么它不起作用以及如何解决它会更好,因为你知道他们说什么,给一个人一条他会吃一天的鱼,教他钓鱼他会吃一辈子。

谢谢你的时间。

4

6 回答 6

3

尝试以这种方式更新您的代码(在 top while 内移动 num、odd、even 初始化):

from random import randint

loop = 0

while loop < 10:
    num = 0
    odd = 0
    even = 0
    loop += 1

    while num < 100:
        num += 1
        rand = randint(1, 1000)
        if rand % 2 == 0:
            even += 1
        else:
            odd += 1
    print "Out of 100 Random Numbers, {0} where even and {1} where Odd".format(even, odd)
于 2013-03-28T10:02:27.950 回答
0

您需要在外循环内移动以下内容:

num = 0
odd = 0
even = 0

否则,内部循环的第二次和后续迭代实际上是无操作的。

于 2013-03-28T10:02:09.107 回答
0

每次通过内部循环后,您都不会重置任何计数器。所以,第一次,它计算该运行的奇数和偶数,直到num达到 100。第二次,当它到达 while 循环的开始时,它检查的值num:它已经是100,所以它永远不会甚至进入循环,并仅打印上一次的计数。

因此,解决它的一种方法是将 , 的设置移动numoddeven循环内部。

话虽如此,在这里使用 while 循环有点奇怪。真的,您应该研究 for 循环,这对于此目的要好得多。

于 2013-03-28T10:02:55.153 回答
0

您忘记在外部 while 循环内(在开始循环之前)重置num(以及even和)的值。第二次,它根本不需要执行内部循环或计算任何随机数,因为 num 已经是 100oddwhile num < 100

于 2013-03-28T10:03:17.593 回答
0

只需在第一个 while 循环开始时将odd, even,声明为 0。 这样,您每次都将重置这些变量,从而为您提供所需的输出。num

你应该最终得到

while loop < 10:
    loop = loop +1
    num = even = odd = 0 # here
    while num < 100:
        num = num + 1
        rand = random.randint(1,1000)
        #print(num)
        if rand % 2 == 0:
            even = even + 1
        else:
            odd = odd + 1
        # grammar
     print ("Out of 100 Random Numbers,",even,"were even and",odd,"were Odd")

打印生成数字总数的另一个版本

loop = 0
even = odd = 0 # change 1
while loop < 10:
    loop = loop +1
    num = 0 # change 2
    while num < 100:
        num = num + 1
        rand = random.randint(1,1000)
        #print(num)
        if rand % 2 == 0:
            even = even + 1
        else:
            odd = odd + 1
        # grammar
    print ("Out of", loop*100,"Random Numbers,",even,"were even and",odd,"were Odd")
于 2013-03-28T10:05:07.480 回答
0

while 循环终止后的变量num=100,因此您需要在外部 while 循环中重置 num=0

于 2013-03-28T10:06:36.153 回答