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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分配错误之前引用的Python局部变量相关的知识,希望对你有一定的参考价值。

我是编程新手,在使用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节。

再次,我对编程非常陌生,因此,我将为此提供任何帮助,我将不胜感激。

谢谢。

答案

我认为您打算做的事如下:当基于用户输入运行程序时,将调用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()

P.S。 Home()方法是不必要的,您可以删除它。

另一答案

尝试将smaller函数放在主函数之外,但是above。另外,在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()

以上是关于分配错误之前引用的Python局部变量的主要内容,如果未能解决你的问题,请参考以下文章

在python中赋值之前引用的局部变量'form'

错误:“分配前引用了局部变量‘myData’”

Pycharm - 禁用'局部变量'xxx'可能在分配之前被引用'

分配前引用的 Django 局部变量“表单”

在赋值之前可能会引用局部变量 - Python

Q:'UnboundLocalError':分配前引用了局部变量'exitErr'