0

I need help finding out how to run my import code from a function!

  sayTime(timespeech)
import time
list = ["whats the time","what's the time","WHAT'S THE TIME?","Whats the time","Whats the time?","What's the time?","WHATS THE TIME?","what's the time?","WHATS THE TIME"]
list2 = ["hi","Hi","Hello","hello","Hey","hey"]
while True:
    answer = input("How can i help: ")

    if answer in list:
      timespeech.sayTime()
    
    if answer in list2:
        time.sleep(0.5)
        print("Hi Max!") 
        time.sleep(2)

As you can see, i'm new to python. time speech is another .py file that when i do import timespeech runs. I'm attempting to run it via a function where it states if answer in list: timespeech.sayTime() i believe that you're meant to have it as def sayTime(timespeech): but i'm not sure what comes after that. Please Help!

  • If you're not sure how to define a function, I strongly recommend that you follow some good Python tutorial, this will help you immensely. – Thierry Lathuille Nov 14 '21 at 18:20
  • try ```from timespeech import sayTime``` – Abhijith Ea Nov 14 '21 at 18:25
  • You do not want to "run your import code from a function". You want to 1) import the module at the start, 2) *write the module in such a way that nothing important happens when you import it* (see the linked duplicate), and 3) *explicitly call* some function (or create an instance of a class, and call a method) from that module. If any of that is unclear, you should probably be following a tutorial first instead. Try putting `python how to use modules` into a search engine, for example. – Karl Knechtel Nov 14 '21 at 18:27
  • "i believe that you're meant to have it as `def sayTime(timespeech):` but i'm not sure what comes after that. " After that comes the stuff that is supposed to happen where you wrote `timespeech.sayTime()`. But no, it should just be `def sayTime():`. – Karl Knechtel Nov 14 '21 at 18:28

1 Answers1

0

If you want to import a function (named sayTime) defined in another .py file (named timespeech.py), you have to import the function via:

from timespeech import sayTime

and then write the rest of your code.

Ruggero
  • 381
  • 1
  • 10