So I have 2 files, fish_life_simulator.py
and menu.py
. fish_life_simulator.py
is the main file and executes other files like menu.py
depending on what happens. So here is the code and how it should work:
import os
os.chdir(os.path.dirname(__file__))
result = exec(open(r'menu.py', encoding='utf-8').read())
print(result)
So at first when the code arrives to result = exec(open(r'menu.py', encoding='utf-8').read())
it executes menu.py
and all is fine, but it could stop for several reasons:
- The player exit the game
- The player entered settings
- The player pressed play
So what I decided to do, is when menu.py
will stop running it will return a value, like 1, 2 or 3, so I tried several methods that have been included in here:
Best way to return a value from a python script
like using return
or sys.exit("some value here")
, but even though I did the part inside of menu.py
, neither of them worked, as when I tried return, result
from result = exec(open(r'menu.py', encoding='utf-8').read())
always was None
for some reason and when I tried sys.exit(1)
for example, result didn't get printed at all, so I was just wandering if it was something I was missing inside of fish_life_simulator.py
, because the part with sending the value should be fine, but the part of receiving it is problematic.