正如over here所见,有两种方法可以将某个内容重复多次。但它似乎对我不起作用,所以我想知道是否有人可以帮助我。
基本上,我想重复以下三次
import random
a = []
w = 0
while w<4:
x = random.uniform(1,10)
print(x)
print(w)
a.append(w+x)
print(a)
w=w+1
根据链接的内容,这就是我所做的,
import random
a = []
w = 0
r = 0
while r < 3:
while w<4:
x = random.uniform(1,10)
print(x)
print(w)
a.append(w+x)
print(a)
w = w+1
r += 1
但这似乎行不通。while循环只重复一次,而不是三次。有人能帮我解决这个问题吗?
发布于 2018-01-13 12:36:56
正如@R2RT所述,您需要在每次r
循环后重置w
。试着这样写:
import random
a = []
w = 0
r = 0
while r < 3:
while w<4:
x = random.uniform(1,10)
print(x)
print(w)
a.append(w+x)
print(a)
w = w+1
r += 1
w = 0
发布于 2018-01-13 13:12:40
要重复某件事一定的次数,您可以:
range
或xrange
对于范围内的i(N):# do something here
while
I=0 while I< n:# do here I += 1
i
无关紧要,你可以改用_
for _ in range(n):# do something here _=0 while _
对于嵌套的while
循环,请记住始终保留其结构:
i = 0
while i < n:
j = 0
while j < m:
# do something in while loop for j
j += 1
# do something in while loop for i
i += 1
发布于 2018-01-13 12:14:07
我看不到
w=w+1
在你的代码中,为什么要删除它?在r=r+1
之前添加w=w+1
。
祝好运。
https://stackoverflow.com/questions/48239714
复制