简体   繁体   English

追加是在程序中多次调用时带来语法错误

[英]Append is bring a syntax error back when called more than once in a program

I am trying to program a simple function. 我正在尝试编写一个简单的函数。 It is a function that takes on different forms depending on the value of proton numbers provided. 该功能根据提供的质子数的值采用不同的形式。

The proton numbers are provided in the list called "Z_min." 质子号在称为“ Z_min”的列表中提供。

What I need to do is to calculate the function and store the values in an array so I can plot the values later. 我需要做的是计算函数并将值存储在数组中,以便以后可以绘制值。

I am a bit rusty in Python, but the way I am trying to store the computed values is by using .append to add it to a list in my python program, but this keeps bringing back a syntax error whenever .append is called more than once inside the for loop. 我在Python中有点生疏,但是我尝试存储计算值的方法是使用.append将其添加到python程序的列表中,但是只要.append被调用的次数超过一旦进入for循环。 However, the way the loop is constructed, .append should only be called once in the body of the loop in a single iteration. 但是,循环构造方式.append仅应在一次迭代中在循环体内调用一次。

Can you please help me figure out why this happening? 您能帮我弄清楚为什么会这样吗?

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)

I'm guessing that probably you might want to use * in a few places: 我猜测您可能想在一些地方使用*

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM