我的代码处于两难境地,首先是我必须编写的练习将实现以下目标
练习
创建文件以完成此练习。编写一个生成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循环,重复部分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
你们中的一位优秀的程序员可以向我解释为什么我得到这个结果和/或修改代码来获得练习的期望结果。
解释为什么它不工作以及如何解决它会更好,因为你知道他们说什么,给一个人他会吃一天的鱼,教他钓鱼他会吃一辈子。
感谢您的时间。
答案 0 :(得分:3)
尝试以这种方式更新您的代码(在顶部移动num,odd,甚至初始化):
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)
答案 1 :(得分:0)
您需要在外部循环中移动以下内容:
num = 0
odd = 0
even = 0
否则内循环的第二次和后续迭代实际上是无操作。
答案 2 :(得分:0)
每次内循环传递后,您不会重置任何计数器。所以,第一次,它计算该运行的几率和均值,直到num
达到100.第二次,当它到达while循环的开始时,它检查{{1的值它已经已经 100,因此它甚至从未进入循环,只打印上一次的计数。
因此,修复它的一种方法是在外部循环内移动num
,num
和odd
的设置。
所有这一切,使用while循环在这里有点奇怪。真的,你应该研究for循环,这对于这个目的来说要好得多。
答案 3 :(得分:0)
您忘记在外部while循环内(在启动num
循环之前)重置even
(以及odd
和while num < 100
)的值。第二次,它不需要执行内部循环或计算任何随机数,因为num已经是100
答案 4 :(得分:0)
只需在第一个while循环的开头声明odd
,even
,num
为0。
这样,您每次都会重置这些变量,为您提供所需的输出。
你最终应该
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")
答案 5 :(得分:0)
在while循环终止后变量num=100
所以你需要在外部while循环中重置num = 0