How to Keep a Program Running Continuously

5 Answers 5

The following will run your program forever until you force it to quit (using Ctrl-C):

                while True:     num = int(input("Enter First Number: "))     print(num / 1440)                              

Or as 0x5453 mentioned, you can use a "poison pill". An input given by the user to kill the loop.

                while True:     response = input("Enter First Number: ")     if response == ":q":         break     print(int(response) / 1440)                              

If the user types :q to the program, it will break out of the forever loop and end your program.

answered Aug 8, 2019 at 17:37

peachykeen's user avatar

7

  • Awesome this worked perfectly for what I needed. Now to figure out how to bring it out into its own little calculator app. I do this function x/1440 probably 500 times a day for my work. Figured I would make this easier for myself.

    user11903615

    Aug 8, 2019 at 17:42

  • Sweet! Please accept the answer if you found it beneficial in order to close the thread. Also, what else were you thinking of adding? I can help point you in the right direction for next steps if you would like.

    Aug 8, 2019 at 17:46

  • The answer should be accepted. Thanks for your patience I was nervous to post on here since I thought people would blast me for being new. So I am using PyCharm and I was wondering if I would be able to "pop out" this script so I can run it on an "App" without having to run it in the IDE. I hope I explained this well enough! Thank you!

    user11903615

    Aug 8, 2019 at 17:50

  • Probably a bit easier than making a whole app or whatever out of such would just be to start terminal (if on a Mac/Linux machine) or powershell (if on a Windows machine) and run your program from there. Are you familiar with navigating to certain directories and running python files with a shell?

    Aug 8, 2019 at 17:59

  • Unfortunately, I am not. I know how to open powershell, but as far as getting a .py file running I am not sure. I was thinking about using PySimpleGUI to just get my knowledge up and running with that. But would also like to learn how to operate powershell. Thank you! You're very helpful; I am thankful.

    user11903615

    Aug 8, 2019 at 18:04

The two easiest solutions are:

  1. Loop until the script is manually killed, e.g. with Ctrl-C.
  2. Loop until the user enters a special value; in your case you could stop on any input that is not a number.

In both cases, I suggest you read up on loops.

answered Aug 8, 2019 at 17:31

0x5453's user avatar

1

  • Awesome will do. Thank you, and sorry if the question is very basic. Just getting my feet wet.

    user11903615

    Aug 8, 2019 at 17:32

              while number!=-1: #kill this by entering -1     number = input("Enter number, press enter")     print(int(number)/1440)  while True: #kill this version with Ctrl+C     number = input("Enter number, press enter")     print(int(number)/1440)                          

answered Aug 8, 2019 at 17:35

Harm Campmans's user avatar

You do this with a while loop. A while loop allows you to run a bit of code while a specific condition is met (== True). You can also make it so that this condition is always evaluated to True allowing the loop to run forever. Here is how you'd do this with your code

              running = True while running:     num1 = int(input("Enter First Number: "))     num2 = int(1440)     result = num1 / num2      print(result)                          

This code will constantly check if running == True which it always will be because there is no condition in the loop that would allow it to become anything else. Running this code will run you bit of code again and again until you press Ctrl + c

answered Aug 8, 2019 at 17:36

Max00355's user avatar

You could set up a loop to run like this...

              minutes_in_day = 1440 user_input = None prompt = "Enter Minutes: " while not user_input == "exit":     try:         user_input = input(prompt)         if user_input == "exit":             continue         user_number = int(user_input)         result = user_number/minutes_in_day         print(result)     except ValueError:         print("Could not find number.")                          

answered Aug 8, 2019 at 17:44

David Culbreth's user avatar

barnetttennesers.blogspot.com

Source: https://stackoverflow.com/questions/57417904/how-to-automatically-keep-a-script-running

Belum ada Komentar untuk "How to Keep a Program Running Continuously"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel