#๐Ÿ”’ Problem while importing a function.

71 messages ยท Page 1 of 1 (latest)

tacit spindle
#

Hi, im trying to import a function from a previous code so i can work with it, the problem is that when i try to do so, it prints somethings on screen despite having no print or calling any function that prints anything

hardy riverBOT
#

@tacit spindle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

steel hinge
#

ok

#

so

#

what does it print?

brazen tangle
tacit spindle
tacit spindle
brazen tangle
#

share the code here

glossy pasture
brazen tangle
#

without looking at the code, we can't tell you what's wrong .. there are multiple guesses we can take but rather look at the code and give a definitive answer ..

glossy pasture
#

https://paste.pythondiscord.com/JF2A

def  impresion(matriz2):
    """
    Por cada casilla de la matriz que le pasemos, va a imprimir un cuadrado, sin cambiar la matriz original
    """
    for fila in range(dimension):
        for columna in range(dimension):
            if matriz2[columna][fila] == 3:
                print(colored("โ–“โ–“", "red"),end='')
            elif matriz2[columna][fila] == 2:
                print(colored("โ–“โ–“", "light_red"),end='')
            elif matriz2[columna][fila] == 1:
                print(colored("โ–“โ–“", "yellow"),end='')
            elif matriz2[columna][fila] == -1:
                print(colored("โ–“โ–“", "green"),end='')
            elif matriz2[columna][fila]== 0:
                print(colored("โ–“โ–“", "white"),end='')
            else:
                print("  ",end='')
        print("")

    print("\n")

lines 59 to 79 contain print()
and you are calling the function in line 150 inside the incendio() function which is being called in the file at line 157, which causes it to be printed on importing

to solve the issue you need move the function call on 157 to a function, and since it is in the main part of the script. I recommend you put it in a main function and add this to the end of your file

if __name__ == "__main__":
    main()

which tells python to run this function (which is basically the main script) only when you run the file itself and not import it from somewhere ele

you got it? ask me if you want to understand more

tacit spindle
#

i tried doing thos

#

like this

#

but in the second file i removed the part when it calls that function

glossy pasture
#

the function will run even if you remove the call from the second file because it is being called from the file itself
and to prevent that you need to wrap it in a main() function

tacit spindle
#

i do not think we are allowed to do that

#

this is for an assignment

#

we cant use some things

glossy pasture
#

so they don't allow you to wrap the code in a main function? that can't be true
acutally it is a good practice to do so

tacit spindle
#

they told us to use only what weยดve seen

glossy pasture
tacit spindle
#

hm

#

ok

#

so, how does it work?

glossy pasture
tacit spindle
#

im sorry, i dont get what you mean

brazen tangle
#

in that case keep everything inside their own function. So, when you import, you import the function directly without calling it. Right now, there are part of the script (in the first file) that are not wrapped in any function. So, as soon as you import it to another file, it will trigger them. If you can't do if __name__ == "__main__: then keep everything as a part of some function.

For example, in the first file keep you while loop at line 161 in a function. So that it will trigger when you import that function in the 2nd file and call the function explicitly.

tacit spindle
#

OOOO

#

ok

#

i think iget it

#

but how do i call it without it running on the second file

brazen tangle
#

Instead of having the following

while burningtrees>0:

    bosque=incendio(bosque)[0]
    burningtrees=incendio(bosque)[1]
    minutos +=1
    print(f"Tiempo: {minutos}")

Do,

def trigger_main():
    while burningtrees>0:

        bosque=incendio(bosque)[0]
        burningtrees=incendio(bosque)[1]
        minutos +=1
        print(f"Tiempo: {minutos}")

This way, you have to import it in the 2nd file and explicitly trigger the function.

tacit spindle
#

bc i need it to run on the firt file

tacit spindle
tacit spindle
glossy pasture
#

and it will only run when you run the file itself, and not import it

tacit spindle
#

OK

glossy pasture
tacit spindle
#

give me a sec

brazen tangle
#

there is a way to create modules and turn your project into a package. Which means, you can't directly run it. Only 1 file should run. All other should only contain functions and classes. They are not supported to run.

glossy pasture
tacit spindle
#

dont celebrate too soon

#

xd

glossy pasture
#

:\

tacit spindle
#

los muertos se cuentan frios

glossy pasture
#

I'm sorry but I don't understand this

tacit spindle
#

its a famous phrase in spanish

glossy pasture
#

can u translate?

tacit spindle
#

its literal translation would be "dont count them as dead until thheir bodies are cold"

glossy pasture
#

ohhhhhh

#

it's good to know
I might learn spanish some time soon

tacit spindle
#

thats nice

glossy pasture
#

โค๏ธ

tacit spindle
#

let me try this

#

with te code

glossy pasture
#

yeah

brazen tangle
tacit spindle
#

let me see

#

what did you change?

brazen tangle
# tacit spindle what did you change?

a lot .. please check it properly .. I wrapped bosque_vacio inside a function. In the 2nd file I remove all extra stuff and simply import the functions from the first file and triggered it. Just compare these with your original code and see the difference.

tacit spindle
#

let me see

#

the first dfile doesnt print anything

brazen tangle
#

Yes it won't. That's the whole point. Break down the code and keep them in separate files to make sure it is organised. And keep only 1 file as a point of entry. This way to trigger anything we have to trigger that 1 specific file.

hardy riverBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.