Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6473747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:30:13+00:00 2026-05-25T06:30:13+00:00

First, sorry for my stupid title :) And here is my problem.. Actually it’s

  • 0

First, sorry for my stupid title 🙂 And here is my problem.. Actually it’s not a problem. Everything works, but I want to have better structure…

I have a python script with a loop “looped” each second.
In the loop there are many many IFs. Is it possible to put each IF in a separate file and then to include it in the loop? So this way every time the loop is “looped”, all the IFs will be passed, too..

There are too many conditions in my script and all of them are different generally from the otheres so I want to have some kind of folder with modules – mod_wheather.py, mod_sport.py, mod_horoscope.py, etc..

Thanks in advance. I hope I wrote everything understandable..

EDIT:
Here is a structural example of what I have now:

while True:
   if condition=='news':
      #do something

   if condition=='sport':
      #so something else

   time.sleep(1)

It will be good if I can have something like this:

while True:
   import mod_news
   import mod_sport

   time.sleep(1)

And these IFs from the first example to be separated in files mod_news.py, mod_sport.py…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:30:13+00:00Added an answer on May 25, 2026 at 6:30 am

    perhaps you wonder how to work with your own modules in general.
    make one file named ‘weather.py’ and have it contain the appropriate if-statements like:

    """ weather.py - conditions to check """
    
    def check_all(*args, **kwargs):
        """ check all conditions """
        if check_temperature(kwargs['temperature']):
            ... your code ...
    
    def check_temperature(temp):
        -- perhaps some code including temp or whatever ...
        return temp > 40
    

    same for sport.py, horoscope.py etc

    then your main script would look like:

    import time, weather, sport, horoscope
    kwargs = {'temperature':30}
    condition = 'weather'
    while True:
        if condition == 'weather':
            weather.check_all(**kwargs)
        elif condition == 'sport':
            sport.check_all()
        elif condition == 'horoscope':
            horoscope.check_all()
        time.sleep(1)
    

    edit: edited according to the edit in your question. Note that I suggest importing all modules only one time, at the beginning of the script, and using its functions. This is better than executing code by importing. But if you insist, you could use reload(weather), which actually performs a reload including code execution. But I cannot stress too much that using functions of external modules is a better way to go!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for stupid title, first. :) Problem is very simple... .gitignore ignores me! Scenario:
First of all Sorry if my question title sounds stupid.... I have the following
First of I'm very sorry but this questions is not so so specific. All
First of all, sorry about that title. I'm not the best at writing those
Sorry, this's my first time to ask a question here. So, I don't have
I am sorry, my question is stupid, but I am not able to answer
First off - sorry for the wall of code but it's not too horrendous,
This is my first question here, so sorry in forward if it is not
Hello first sorry if its a stupid question, but I amm a beginner, and
First sorry for my English I have a problem with JSON. Assume the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.