How to use user input to end a program?

I am new to python and I am writing a program that converts millimeters to Inches. It is basically a continuous loop that allows you to store numbers and get the correct transformed measurement. I want to put an IF statement that allows the user to type "end" to end the program instead of converting more units. How will I do this work (which 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) 
+4
source share
6 answers

Just use the 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) 

Thus, iter calls input and stores the return value in i until i == 'end' .

Note that you probably want to check for some errors if the user enters a non-numeric value, as in the example above.

+4
source

To end the loop, you can use the break statement. This can be used inside the if , since break only considers loops, not conditional ones. So:

 if user_input == "end": break 

Notice how I used user_input , not MM ? This is because your code has a slight problem right now: you call float () before you ever check what the user typed. This means that if they type "end", you will 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 

Another improvement you can make: if you want the user to be able to enter "END" or "End" or "end", you can use the lower () method to convert the input to lower case 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 of these changes and your program will work the way you want.

+4
source

You can use break to exit the loop.

 MM=input() if MM == "end": break 
+2
source

To complete the loop, you can use break statement . You can also take advantage of the fact that a ValueError raised if a 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 go only to the exit line and go to the next loop if an error occurs, a good way would be:

 MM = input() if MM == 'exit': break try: MM = float(MM) except ValueError: print( 'I\'m sorry {} isn\'ta valid value'.format(MM)) continue # Next iteration 

Or you can do it "linuxy", you can wait until Ctrl+C (keyboard interrupt) is pressed and process it gracefully:

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

What would look like this ( ^C means sending Ctrl+C ):

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

Testing for MM after user input may work. You use the break keyword to exit the loop. Your example, after 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) 
+1
source
 import os print ("Convert MM to Inches") convert=float(25.4) while True: print("*******MM*******") MM=input() if MM.lower() in ('end', 'quit'): os._exit(1) MM = float(MM) Results=float(MM/convert) print("*****Inches*****") print("%.3f" % Results) print("%.4f" % Results) print("%.5f" % Results) 

or

 print ("Convert MM to Inches") convert=float(25.4) while True: print("*******MM*******") MM=input() if MM.lower() in ('end', 'quit'): break MM = float(MM) Results=float(MM/convert) print("*****Inches*****") print("%.3f" % Results) print("%.4f" % Results) print("%.5f" % Results) 
0
source

All Articles