pythonfor循环100次_在for循环中只打印一次
選項1:您可以使用下面這樣的else條件。在def Input_Q_bounds(lower, upper):
delta_x = .1
# since there are 100 iterations
J = np.zeros(101)
for i in range(101):
Q_i = (i * delta_x) + (delta_x / 2)
if lower <= Q_i <= upper:
Q = 1
else:
Q = 0
# now fill the matrix
J[i] = (Q + (9.5 * (J[i - 1]))) / 10.5
J_analytical = Q * (np.exp(upper - 10) + (np.exp(lower - 10))
else:
print(J_analytical)
print(J[100])
if __name__ == "__main__":
Input_Q_bounds(lower, upper)
選項2:下面的解決方案是使用全局變量
^{pr2}$
選項3:從函數返回值。在def Input_Q_bounds(lower, upper):
delta_x = .1
# since there are 100 iterations
J = np.zeros(101)
for i in range(101):
Q_i = (i * delta_x) + (delta_x / 2)
if lower <= Q_i <= upper:
Q = 1
else:
Q = 0
# now fill the matrix
J[i] = (Q + (9.5 * (J[i - 1]))) / 10.5
J_analytical = Q * (np.exp(upper - 10) + (np.exp(lower - 10))
return J[100], J_analytical
if __name__ == "__main__":
Input_Q_bounds(lower, upper)
總結
以上是生活随笔為你收集整理的pythonfor循环100次_在for循环中只打印一次的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两个列表合并去重_把两个pdf合并成一个
- 下一篇: websocket python爬虫_p