0

I am new to programming (obviously) and am looking to make a script where I can input a number and have it automatically divided by 1440 (the number of minutes in a day). I want this to be able to repeat itself over and over again. For example I enter 400 and it divides it by 1440. I then want to be able to enter another number directly after that without having to restart the script. How would I go about this? Can anyone lead me in the correct direction.

num1 = int(input("Enter First Number: "))

num2 = int(1440)

result = num1 / num2

print(result)
0x5453
  • 12,753
  • 1
  • 32
  • 61
  • 1
    Yeah I tried using a while loop but could not figure out how they worked, I understand your point. It is not my goal to not learn how to code. I want to learn so I don't have to ask. –  Aug 08 '19 at 18:10

5 Answers5

1

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.

peachykeen
  • 4,143
  • 4
  • 30
  • 49
  • 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. –  Aug 08 '19 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. – peachykeen Aug 08 '19 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! –  Aug 08 '19 at 17:50
  • 1
    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? – peachykeen Aug 08 '19 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. –  Aug 08 '19 at 18:04
  • PySimpleGUI would be cool to check out. To run a python script with powershell, first open powershell. Next, navigate to the directory (folder) where your script file (the file you saved with the .py extension) is by typing the following: `cd "C:\path\to\directory"`. So for example, if my script file was on my Desktop, I would type `cd "C:\Users\username\Desktop"`. Then, to run the script, you should type: `python filename.py` where you replace filename with what you named the file. – peachykeen Aug 08 '19 at 18:13
  • Awesome! I will check and look into both of these. I thank you for your help today! It makes me want to keep getting better knowing there are so many resources I can use.. –  Aug 08 '19 at 18:42
0

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.

0x5453
  • 12,753
  • 1
  • 32
  • 61
  • Awesome will do. Thank you, and sorry if the question is very basic. Just getting my feet wet. –  Aug 08 '19 at 17:32
0
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)
0

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

Max00355
  • 827
  • 2
  • 12
  • 28
0

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.")
David Culbreth
  • 2,610
  • 16
  • 26