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)
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)
Make all of these changes and your program will work the way you want.
rmunn source share