繁体   English   中英

分配错误之前引用的 Python 局部变量

[英]Python Local Variable Referenced Before Assignment Error

我是编程新手,在使用 def 函数时遇到了一些困难。

我正在创建一个程序,该程序将要求用户选择他们选择的子程序,他们将通过输入 1、2 或 3 来表示。

当用户选择他们的程序时,将提示用户输入程序求解方程所需的所需输入。

def main():
    def Home():
        print('''Please select a program.
        1: Linear Equation Solver
        2: Kirchoff's Laws Solver
        3: Voltage Divider ''')

        Program_Selection = int(input("Please select your program: "))
        if Program_Selection == 1:
            LES()
        if Program_Selection == 2:
            KCL()
        if Program_Selection == 3:
            VD()
    Home()

    def KCL():
        Voltage_Source_1 = int(input("Voltage Source 1: "))
        Voltage_Source_2 = int(input("Voltage Source 2: "))

        Resistor_1 = int(input("Resistor 1: "))
        Resistor_2 = int(input("Resistor 2: "))
        Resistor_3 = int(input("Resistor 3: "))

        Factor = ( - (Resistor_1 + Resistor_3) / Resistor_3)

        Current_2 = (Voltage_Source_1 + (Factor * Voltage_Source_2)) / (((Resistor_3 + Resistor_2) * Factor) + Resistor_3) * 1000
        Current_1 = (Voltage_Source_1 - (Resistor_3 * (Current_2 / 1000))) / (Resistor_1 + Resistor_3) * 1000
        Current_3 = Current_1 + Current_2

        Voltage_Resistor_1 = abs(((Current_1) / 1000) * Resistor_1)
        Voltage_Resistor_2 = abs(((Current_2) / 1000) * Resistor_2)
        Voltage_Resistor_3 = abs(((Current_3) / 1000) * Resistor_3)

        Power_Resistor_1 = abs(Current_1 * Voltage_Resistor_1)
        Power_Resistor_2 = abs(Current_2 * Voltage_Resistor_1)
        Power_Resistor_3 = abs(Current_3 * Voltage_Resistor_1)

        print("Current 1:", round(Current_1, 3), "mA.")
        print("Current 2:", round(Current_2, 3), "mA.")
        print("Current 3:", round(Current_3, 3), "mA.")

        print("Resistor 1 Voltage:", round(Voltage_Resistor_1, 3), "V.")
        print("Resistor 2 Voltage:", round(Voltage_Resistor_2, 3), "V.")
        print("Resistor 3 Voltage:", round(Voltage_Resistor_3, 3), "V.")

        print("Resistor 1 Power:", round(Power_Resistor_1, 3), "mW.")
        print("Resistor 2 Power:", round(Power_Resistor_2, 3), "mW.")
        print("Resistor 3 Power:", round(Power_Resistor_3, 3), "mW.")
    while True:
        KCL()
        if input("Would you like to repeat the program? (Yes / No): ") == "Yes":
            KCL()
        else:
            Home()

main()

所以我明白,为了让我调用 KCL 部分,我必须先定义它,所以我尝试将它放在 def Home(): 部分之前; 但是,这会导致在 Home() 部分之前调用 KCL 部分。

再一次,我对编程很陌生,所以我很感激我能得到的任何帮助。

谢谢你。

尝试将较小的函数放在主函数之外,但放在主函数之上 此外,在 python 中,您不必将程序的主要部分包装在 main 函数中。

def Home():
    print('''Please select a program.
    1: Linear Equation Solver
    2: Kirchoff's Laws Solver
    3: Voltage Divider ''')

    Program_Selection = int(input("Please select your program: "))
    if Program_Selection == 1:
        LES()
    if Program_Selection == 2:
        KCL()
    if Program_Selection == 3:
        VD()
def KCL():
    Voltage_Source_1 = int(input("Voltage Source 1: "))
    Voltage_Source_2 = int(input("Voltage Source 2: "))

    Resistor_1 = int(input("Resistor 1: "))
    Resistor_2 = int(input("Resistor 2: "))
    Resistor_3 = int(input("Resistor 3: "))

    Factor = ( - (Resistor_1 + Resistor_3) / Resistor_3)

    Current_2 = (Voltage_Source_1 + (Factor * Voltage_Source_2)) / (((Resistor_3 + Resistor_2) * Factor) + Resistor_3) * 1000
    Current_1 = (Voltage_Source_1 - (Resistor_3 * (Current_2 / 1000))) / (Resistor_1 + Resistor_3) * 1000
    Current_3 = Current_1 + Current_2

    Voltage_Resistor_1 = abs(((Current_1) / 1000) * Resistor_1)
    Voltage_Resistor_2 = abs(((Current_2) / 1000) * Resistor_2)
    Voltage_Resistor_3 = abs(((Current_3) / 1000) * Resistor_3)

    Power_Resistor_1 = abs(Current_1 * Voltage_Resistor_1)
    Power_Resistor_2 = abs(Current_2 * Voltage_Resistor_1)
    Power_Resistor_3 = abs(Current_3 * Voltage_Resistor_1)

    print("Current 1:", round(Current_1, 3), "mA.")
    print("Current 2:", round(Current_2, 3), "mA.")
    print("Current 3:", round(Current_3, 3), "mA.")

    print("Resistor 1 Voltage:", round(Voltage_Resistor_1, 3), "V.")
    print("Resistor 2 Voltage:", round(Voltage_Resistor_2, 3), "V.")
    print("Resistor 3 Voltage:", round(Voltage_Resistor_3, 3), "V.")

    print("Resistor 1 Power:", round(Power_Resistor_1, 3), "mW.")
    print("Resistor 2 Power:", round(Power_Resistor_2, 3), "mW.")
    print("Resistor 3 Power:", round(Power_Resistor_3, 3), "mW.")

#This is where the program starts, calling the above functions
Home()

while True:
    KCL()
    if input("Would you like to repeat the program? (Yes / No): ") == "Yes":
        KCL()
    else:
        Home()

我认为您打算做的事情如下:当您根据用户输入运行程序时,将调用 main 函数,它将运行其他方法之一。 之后,在每个方法结束时,它会再次要求用户输入,如果是,它将再次运行该方法(我相信不需要 while 循环)。 我编辑了你的代码,它应该是这样的。

def main():
    print('''Please select a program.
    1: Linear Equation Solver
    2: Kirchoff's Laws Solver
    3: Voltage Divider ''')

    Program_Selection = int(input("Please select your program: "))
    if Program_Selection == 1:
        LES()
    if Program_Selection == 2:
        KCL()
    if Program_Selection == 3:
        VD()


def KCL():
    Voltage_Source_1 = int(input("Voltage Source 1: "))
    Voltage_Source_2 = int(input("Voltage Source 2: "))

    Resistor_1 = int(input("Resistor 1: "))
    Resistor_2 = int(input("Resistor 2: "))
    Resistor_3 = int(input("Resistor 3: "))

    Factor = ( - (Resistor_1 + Resistor_3) / Resistor_3)

    Current_2 = (Voltage_Source_1 + (Factor * Voltage_Source_2)) / (((Resistor_3 + Resistor_2) * Factor) + Resistor_3) * 1000
    Current_1 = (Voltage_Source_1 - (Resistor_3 * (Current_2 / 1000))) / (Resistor_1 + Resistor_3) * 1000
    Current_3 = Current_1 + Current_2

    Voltage_Resistor_1 = abs(((Current_1) / 1000) * Resistor_1)
    Voltage_Resistor_2 = abs(((Current_2) / 1000) * Resistor_2)
    Voltage_Resistor_3 = abs(((Current_3) / 1000) * Resistor_3)

    Power_Resistor_1 = abs(Current_1 * Voltage_Resistor_1)
    Power_Resistor_2 = abs(Current_2 * Voltage_Resistor_1)
    Power_Resistor_3 = abs(Current_3 * Voltage_Resistor_1)

    print("Current 1:", round(Current_1, 3), "mA.")
    print("Current 2:", round(Current_2, 3), "mA.")
    print("Current 3:", round(Current_3, 3), "mA.")

    print("Resistor 1 Voltage:", round(Voltage_Resistor_1, 3), "V.")
    print("Resistor 2 Voltage:", round(Voltage_Resistor_2, 3), "V.")
    print("Resistor 3 Voltage:", round(Voltage_Resistor_3, 3), "V.")

    print("Resistor 1 Power:", round(Power_Resistor_1, 3), "mW.")
    print("Resistor 2 Power:", round(Power_Resistor_2, 3), "mW.")
    print("Resistor 3 Power:", round(Power_Resistor_3, 3), "mW.")

   if input("Would you like to repeat the program? (Yes / No): ") == "Yes":
        KCL()
  else:
        main()

main()

PS Home() 方法是不必要的,您可以将其删除

暂无
暂无

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

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