【发布时间】:2018-11-25 22:48:35
【问题描述】:
我已经定义了一些数据,这些数据会附加到一些列表中。为了做到这一点,我需要将它们放入某种嵌套循环条件中。首先创建一个 1-15 之间的范围,然后计算 rebarnumber,在循环中,我在匹配 n <= rebarnumber 时设置这些条件,*do something*,然后在 n >= rebarnumber do something else 时继续。问题是当上述条件满足时,我会做除了得到全长范围数字的列表。
反而得到了这个结果。
[49.0] 1
[49.0, 49.0] 2
[49.0, 49.0, 49.0] 3
[49.0, 49.0, 49.0, 49.0] 4
[49.0, 49.0, 49.0, 49.0, 49.0] 5
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0] 6
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0] 7
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0] 8
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 9
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 10
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 11
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 12
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 13
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 14
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 49.0, 84.0] 15
想要的结果,(仅显示最后 2 行打印)
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0] 14
[49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 49.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0, 84.0] 15
代码:
h = 300
cb = 35
ct = 35
ca = 35
b= 300
y = 12
d = h - cb
ds = ct
a = 25
yb = 8
rebarnumber = math.floor((b-(2*cb+2*yb+y))/a)
disc = []
dis = []
Asi = []
Asci = []
for n in range(1,16):
if n <= rebarnumber+1:
Asi.append(int(3.1416/4*(y)**2))
dis.append( h - (cb + yb + y/2 ))
Asci.append(int(3.1416/4*(y)**2))
disc.append( ct + yb + y/2 )
if n >= rebarnumber:
Asi.append(int(3.1416/4*(y)**2))
dis.append( h - (cb + yb + y/2 ) - ca)
Asci.append(int(3.1416/4*(y)**2))
disc.append( cb + yb + y/2 + ca)
print(disc, n)
我做错了什么?!任何帮助!
【问题讨论】:
-
这取决于你想要发生什么。你展示了你不想要的东西,但没有清楚地描述你想要什么。你能显示你想要的输出吗?你试过调试吗?
-
你说的是嵌套循环,其实只有一个循环。
-
查看我更新的图片
-
可能您必须将第二个
if块取消缩进一级(并可能调整条件)。 -
我想改变条件,否则语句不会在这里使用。但我想保持简单。
标签: python python-3.x list for-loop