How to use user input to end a program?

27,896

Solution 1

To end the loop, you can use a break statement. This can be used inside an if statement, since break only looks at loops, not conditionals. So:

if user_input == "end":
    break

Notice how I used user_input, not MM? That's because your code has a minor problem right now: you're calling float() before you ever check what the user typed. Which means if they type "end", you'll call float("end") and get an exception. Change your code to something like this:

user_input = input()
if user_input == "end":
    break
MM = float(user_input)
# Do your calculations and print your results

One more improvement you can make: if you want the user to be able to type "END" or "End" or "end", you can use the lower() method to convert the input to lowercase before comparing it:

user_input = input()
if user_input.lower() == "end":
    break
MM = float(user_input)
# Do your calculations and print your results

Make all those changes, and your program will work the way you want it to.

Solution 2

Just use iter with a sentinel:

print ("Convert MM to Inches")
convert=float(25.4)
for i in iter(input, 'end'):
    print("*******MM*******")
    try:
        MM=float(i)
    except ValueError:
        print("can't convert {}".format(i)) 
    else:
        Results=float(MM/convert)
        print("*****Inches*****")
        print("%.3f" % Results)
        print("%.4f" % Results)
        print("%.5f" % Results)

This way, iter calls input and stores the return value of it in i until i == 'end'.

Note that you probably want some error checking in case the user enters a non numeric value like in the example above.

Solution 3

You can use break to break out of the loop.

MM=input()
if MM == "end":
    break

Solution 4

To end loop you could use break statement. You could also take advantage of fact that ValueError is raised if non convertible value is issued:

print ("Convert MM to Inches")
convert=float(25.4)
while True:
    print("*******MM*******")
    MM=input()
    try:
        MM = float(MM)
    except ValueError:
        break

    Results=float(MM/convert)
    print("*****Inches*****")
    print("%.3f" % Results)
    print("%.4f" % Results)
    print("%.5f" % Results)

Result:

[vyktor@grepfruit tmp]$ ./convert 
Convert MM to Inches
*******MM*******
exit
[vyktor@grepfruit tmp]$ 

Or if you want to exit only on string exit and go to next loop if error occurs, nice way would be:

MM = input()
if MM == 'exit':
    break

try:
    MM = float(MM)
except ValueError:
    print( 'I\'m sorry {} isn\'t a valid value'.format(MM))
    continue # Next iteration

Or you could make it "linuxy" you can wait until Ctrl+C (Keyboard interrupt) is pressed and handle it gracefully:

try:
    # Whole program goes here
except KeyboardInterrupt:
    print('Bye bye')

Which will go like this (^C means sending Ctrl+C):

[vyktor@grepfruit tmp]$ ./convert 
Convert MM to Inches
*******MM*******
^CBye bye

Solution 5

Testing for MM after it is input by the user could work. You use the keyword break to break out of the loop. Your example, after the minor additions is as follows.

print ("Convert MM to Inches")
convert=float(25.4)
while True:
    print("*******MM*******")
    MM=input()
    if MM == 'end':
        break
    Results=float(MM)/convert
    print("*****Inches*****")
    print("%.3f" % Results)
    print("%.4f" % Results)
    print("%.5f" % Results)
Share:
27,896
Inhale.Py
Author by

Inhale.Py

I'm just a normal person who likes to find the easiest way to do things.

Updated on April 26, 2020

Comments

  • Inhale.Py
    Inhale.Py about 4 years

    I'm new to python and I am writing a program that converts Millimeters to Inches. Basically its a continuous loop that allows you to keep putting in numbers and get the correct converted measurement. I want to put an IF statement that will allow the user to type "end" to end the program instead of converting more units of measurement. How would I go about making this work (what python code allows you to exit a written program and can be used in an IF statement.)?

    convert=float(25.4)
    while True:
        print("*******MM*******")
        MM=float(input())
        Results=float(MM/convert)
        print("*****Inches*****")
        print("%.3f" % Results)
        print("%.4f" % Results)
        print("%.5f" % Results)