Python: import file not working in the if statement
Question:
This is a main file in which I start an other.py script.
The initial import works, it executes the other.py on line 1.
But when this has ran the other.py, it will ask input to to restart other.py once more.
The ‘no’ answer is working, the ‘yes’ answer just keeps giving me the same prompt.
So the import is not working in my if statement.
Any ideas please?
import other
while True:
prompt = input("Restart? Yes / No: ")
if prompt == "yes" or prompt == "Yes":
import other
elif prompt == "no" or prompt == "No":
print("Goodbye!")
break
Answers:
Once python has imported a module it won’t import it again. You have to specifically ask python to reload a module if you want it to reload it.
Try and remove the import other
from the top and run your app.
This is a main file in which I start an other.py script.
The initial import works, it executes the other.py on line 1.
But when this has ran the other.py, it will ask input to to restart other.py once more.
The ‘no’ answer is working, the ‘yes’ answer just keeps giving me the same prompt.
So the import is not working in my if statement.
Any ideas please?
import other
while True:
prompt = input("Restart? Yes / No: ")
if prompt == "yes" or prompt == "Yes":
import other
elif prompt == "no" or prompt == "No":
print("Goodbye!")
break
Once python has imported a module it won’t import it again. You have to specifically ask python to reload a module if you want it to reload it.
Try and remove the import other
from the top and run your app.