Reputation: 45
I'm a newbie, writing 2 functions in separate files (weather.py, TTS_phrase.py) in the same directory, to then use together:
def weather_now(my_town)
[My code]
return ("Sunny skies")
city = input("City Name : ")
print(weather_now(city))
and
def talk(text_to_convert)
[My code].save(filename)
print("Done")
t = input("What text would you like to convert to speech?\n")
talk(t)
They both work fine independently. When I create a new py file, import both functions ONLY, not the parts after it, it still seems to run the whole py file, not JUST the function.
import weather as w
import TTS_phrase as TTS
text1 = w.weather_now("London")
TTS.talk(text1)
The indents are correct. It's asking me for inputs and tries to save 2 files when I run this code. Am I doing something wrong? I'd really appreciate a steer. Thanks.
Upvotes: 1
Views: 46
Reputation: 5745
first you must understand that whenever a file is imported the whole script must run! so you can make a check, and exclude code you dont run on execute like this:
def weather_now(my_town)
[My code]
return ("Sunny skies")
if __name__ == "__main__":
city = input("City Name : ")
print(weather_now(city))
and
def talk(text_to_convert)
[My code].save(filename)
print("Done")
if __name__ == "__main__":
t = input("What text would you like to convert to speech?\n")
talk(t)
Upvotes: 2