1

How do I loop back to previous user input(width) instead of asking user to re-enter the height variable again after ValueError is triggered.

while True:
    try:
        l=int(input("Length : "))
        w=int(input("Width : "))
        area=l*w
        perimeter=2*(l+w)
        print("Area of Rectangle : ",area)
        print("Perimeter of Rectangle : ",perimeter)
    except ValueError:
 
       print("Enter a numeric value")

Output: Output

1 Answer 1

1

One possible solution is to create 2 while loops, each, with one input.

while True:
    try:
        l=int(input("Length : "))
        break
    except ValueError:
        print("Enter a numeric value")
while True:
    try:
        w=int(input("Width : "))
        break
    except ValueError:
        print("Enter a numeric value")
area=l*w
perimeter=2*(l+w)
print("Area of Rectangle : ",area)
print("Perimeter of Rectangle : ",perimeter)    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.