gpt4 book ai didi

python - 追加在程序中多次调用时会返回语法错误

转载 作者:行者123 更新时间:2023-12-01 00:34:06 24 4
gpt4 key购买 nike

我正在尝试编写一个简单的函数。它是一个根据所提供的质子数的值而呈现不同形式的函数。

质子数在名为“Z_min”的列表中提供。

我需要做的是计算函数并将值存储在数组中,以便稍后绘制值。

我对 Python 有点生疏,但我尝试存储计算值的方式是使用 .append 将其添加到我的 python 程序中的列表中,但是每当 .append 时,这都会带回语法错误在 for 循环内多次调用。但是,根据循环的构造方式,.append 只能在单次迭代的循环体中调用一次。

您能帮我弄清楚为什么会发生这种情况吗?

import math as m
import matplotlib.pyplot as plt
import numpy as np

a_V = 15.75
a_S = 17.8
a_C = 0.711
a_A = 23.695
a_P = 11.2

Z_min = []
A = []
BA = []

for A_idx in range(0,301):
Z_min_val = (A_idx/2) * (1 +(a_C/(4*a_A))*A_idx**(2/3))**(-1)
A.append(A_idx)
Z_min.append(round(Z_min_val))


for i in Z_min:
if (A[i]%2 == 0):
if (Z_min[i]%2) == 0:
#print("This is even: ", Z_min[i])
if (A[i] == 0):
BA_val = 0
BA.append(BA_val)
else:
BA_val = (a_V*A[i] - a_S*(A[i])**(2/3) - a_C*(Z_min[i](Z_min[i]-1))/((A[i])**(1/3)) - a_A((A[i]-2*Z_min[i])**2)/(A[i]) + a_P*(A[i])**(-1/2))
BA.append(BA_val)
else:
#print("This is odd: ", Z_min[i])
BA_val = (a_V*A[i] - a_S*(A[i])**(2/3) - a_C*(Z_min[i](Z_min[i]-1))/((A[i])**(1/3)) - a_A((A[i]-2*Z_min[i])**2)/(A[i]) - a_P*(A[i])**(-1/2))
BA.append(BA_val)
else:
B_by_A_val = (a_V*A[i] - a_S*(A[i])**(2/3) - a_C*(Z_min[i](Z_min[i]-1))/((A[i])**(1/3)) - a_A((A[i]-2*Z_min[i])**2)/(A[i]))
B_by_A.append(B_by_A_val)

最佳答案

我猜您可能想在一些地方使用 *:

import math as m
import matplotlib.pyplot as plt
import numpy as np

a_V = 15.75
a_S = 17.8
a_C = 0.711
a_A = 23.695
a_P = 11.2

Z_min = []
A = []
BA = []
B_by_A = []

for A_idx in range(0,301):
Z_min_val = (A_idx/2) * (1 +(a_C/(4*a_A))*A_idx**(2/3))**(-1)
A.append(A_idx)
Z_min.append(round(Z_min_val))


for i in Z_min:
if (A[i]%2 == 0):
if (Z_min[i]%2) == 0:
#print("This is even: ", Z_min[i])
if (A[i] == 0):
BA_val = 0
BA.append(BA_val)
else:
BA_val = (a_V*A[i] - a_S*(A[i])**(2/3) - a_C*(Z_min[i]*(Z_min[i]-1))/((A[i])**(1/3)) - a_A*((A[i]-2*Z_min[i])**2)/(A[i]) + a_P*(A[i])**(-1/2))
BA.append(BA_val)
else:
#print("This is odd: ", Z_min[i])
BA_val = (a_V*A[i] - a_S*(A[i])**(2/3) - a_C*(Z_min[i]*(Z_min[i]-1))/((A[i])**(1/3)) - a_A*((A[i]-2*Z_min[i])**2)/(A[i]) - a_P*(A[i])**(-1/2))
BA.append(BA_val)
else:
B_by_A_val = (a_V*A[i] - a_S*(A[i])**(2/3) - a_C*(Z_min[i]*(Z_min[i]-1))/((A[i])**(1/3)) - a_A*((A[i]-2*Z_min[i])**2)/(A[i]))
B_by_A.append(B_by_A_val)

关于python - 追加在程序中多次调用时会返回语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57940901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com