Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Loop This Code
#1
#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.
Yoriz write Dec-09-2021, 08:47 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020