0

I wanted to have some sort of thing that if the user inputs "Quit", the program breaks. I think that I could achieve that with a do while loop but I don't understand how to implement it. Please help me.

num1 = float(input("Enter First Number: "))
num2 = float(input("Enter second number: "))
op = input("Enter Operator: ")

if op == "*":
    print(num1 * num2)

elif op == "+":
    print(num1 + num2)

elif op == "-":
    print(num1 - num2)

elif op == "/":
    print(num1 / num2)

elif op == "%":
    print(num1 % num2)

else:
    print("Invalid Operator")
Brain Gym
  • 21
  • 1
  • 2
  • 2
    Welcome to StackOverflow! I see you're a new contributor, so I advise you to check out [How to ask a good question](https://stackoverflow.com/help/how-to-ask), [How to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), and [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/a/284237/11082165). – Brian61354270 Apr 04 '21 at 15:39
  • https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response this might give you some kind of orientation – dot.Py Apr 04 '21 at 15:42

2 Answers2

1
while True:
    try:
        #insert your "loop" here
        [.....]
        
    except ValueError:
        #restart the loop
        continue

    else:
        #exit the loop
        break
dot.Py
  • 5,007
  • 5
  • 31
  • 52
0
while True:
    while True:
        num1 = input("Enter First Number: ")
        if num1.lower() == "quit":
            quit()
        try:
            num1 = int(num1)
            break
        except ValueError:
            print(f"{num1} is not an integer.")

    while True:
        num2 = input("Enter second number: ")
        if num2.lower() == "quit":
            quit()
        try:
            num2 = int(num2)
            break
        except ValueError:
            print(f"{num2} is not an integer.")

    op = input("Enter Operator: ")
    if op.lower() == "quit":
        quit()

    if op == "*":
        print(num1 * num2)
    elif op == "+":
        print(num1 + num2)
    elif op == "-":
        print(num1 - num2)
    elif op == "/":
        print(num1 / num2)
    elif op == "%":
        print(num1 % num2)
    else:
        print("Invalid Operator")
will-hedges
  • 1,254
  • 1
  • 9
  • 18
  • explanation please? I don't understand do while loops. – Brain Gym Apr 04 '21 at 17:44
  • @BrainGym a `while` loop runs while a condition is met. In this case, `while True:` is effectively an infinite loop (`while True` will always be `True`). What I've done is have each step run until the input is valid, *or* until the user enters "quit." `break` will break the program out of the infinite loop. So this program runs each step until it receives a valid input or the user enters "quit." I recommend reading [ATBSWP Chapter 2](https://automatetheboringstuff.com/2e/chapter2/) for a more thorough explanation. – will-hedges Apr 06 '21 at 18:49