1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Importing variables from another file [closed]

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 09:02.

  1. Stack

    Stack Membro Participativo

    And so... I imported variables(from table1 import x, etc) from one file to another and ran into the problem of not being able to import them. If the file has, for example, x = 10, then everything works fine. But if x = 1 + 1(Or something like that) in the file, an error is returned: ImportError: cannot import name 'x' from 'table1'.

    As I understand it, the imported function does not save the value of the variable and therefore the program simply cannot find a non-existing variable. So... how can I import a calculated variable into the main file? :) And in general, where could I learn more about the intricacies of importing, if anyone knows.

    Upd: I did some work and it looks like this:

    table1.py:

    def table_n(s23, f1):
    if s23 < 100:
    if f1 == 50:
    if s23 in range(0, 2):
    print("Out of limit")
    exit()
    elif s23 in range(2, 15):
    print("2-15")
    n = 0.5
    elif s23 in range(15, 50):
    print("15-50")
    n = 0.6
    elif s23 in range(50, 150):
    print("50-150")
    n = 0.8
    elif s23 in range(150, 300):
    print("150-300")
    n = 0.9
    elif s23 in range(300, 1000):
    print("300-1000")
    n = 0.93
    elif s23 in range(1000, 2500):
    print("1000-2500")
    n = 1
    else:
    print("Out of limit")
    exit()


    program.py:

    f1 = float(50)
    s2 = float(15)
    s3 = float(1)
    s23 = s2 + s3

    from table1 import table_n
    table_n(s23, f1)
    from table1 import n

    print(n)


    The variable here is n, not x, but not the point. Print works correctly, but I do not know how to import the variable n into the main file without an error.

    Continue reading...

Compartilhe esta Página