I
I
i3stone2021-11-05 19:40:34
Python
i3stone, 2021-11-05 19:40:34

How can one .py file run a function from another file?

How can I run a function from another file?
type

from main import main # импорт нужной функции

start_game = input("start game? yes/no")

if start_game == "yes":
    main()

elif start_game == "no":
    quit()


I want to make the game menu, but this method, when imported, immediately starts the file and the Main function,
and I need this file to start the main function only when I let you know about it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-11-05
@i3stone

when importing, the file is immediately launched

And there is. For this, there is a construction Remove unnecessary code from the main file under this if if __name__ == '__main__':

P
Python Newbie, 2021-11-05
@Levman5

Do it like this:
Replace with and withfrom main import mainimport main
main()main.main()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question