Python Forum

Full Version: How to Loop This Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#Addition
print ("Hello User, Eneter numbers to add ")
num1 = float (input ("Eneter first numner, "))
num2 = float (input ("Eneter second numner, "))
x = num1 + num2
print (x)
Hello, can you tell me how to loop this program so it will always will ask for a new inputs after its finish the first iteration
Thanks.
Please use code tags when posting.

while True:
    try:
        num1 = float(input('num1 >> '))
        num2 = float(input('num2 >> '))
        print(num1+num2)

    except (ValueError):
        print('Error')
        break
It doesn't make any sense to catch the error and break out of the program. This will continue until you press ctrl+c. If you type something other than a number the program prints the error message but continues running.
while True:
    try:
        print(float(input('num1 >> ')) + float(input('num2 >> ')))
    except ValueError as msg:
        print(msg)