Python Forum
How to Loop This Code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to Loop This Code (/thread-35751.html)



How to Loop This Code - Michael1 - Dec-09-2021

#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.


RE: How to Loop This Code - menator01 - Dec-09-2021

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



RE: How to Loop This Code - deanhystad - Dec-09-2021

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)