'This code keeps looping when the variable "op" is not in the "valid" list [closed]

    valid = (None, "add", "sub", "mul", "div")
    num = 0

    operation = "add"

    print("Operations: add(addition), sub(subtraction), mul(multiplication), div(division), adv(advanced)")

    op = None

    while True:

     print(num)

     op = str(input("Math operation: "))

     while True:

    print(num)

    if op not in valid:

  print("invalid operation!")

  op = None

  continue

if op == "add":

  operation = "add"

  num_ = float(input("Add: "))

  if num_ != 0:

    num += num_

    continue

  else:

    break

    continue

elif op == "sub":

  operation = "sub"

  num_ = float(input("Subtract: "))

  if num_ != 0:

    num -= num_

    continue

  else:

    break

    continue

elif op == "mul":

  operation = "mul"

  num_ = float(input("Multiply: "))

  if num_ != 0:

    num *= num_

    continue

  else:

    break

    continue

elif op == "div":

  operation = "div"

  num_ = float(input("Divide: "))

  if num_ != 0:

    num /= num_

    continue

  else:

    break


Solution 1:[1]

The correct indentation(I think)

valid = (None, "add", "sub", "mul", "div")
num = 0
  

  
operation = "add"

print("Operations: add(addition), sub(subtraction), mul(multiplication), div(division), adv(advanced)")

op = None

while True:

    print(num)

    op = str(input("Math operation: "))

    while True:

        print(num)

        if op not in valid:

            print("invalid operation!")

            op = None

            continue

        if op == "add":

            operation = "add"

            num_ = float(input("Add: "))

            if num_ != 0:

                num += num_

                continue

            else:

                break

                continue

        elif op == "sub":

            operation = "sub"

            num_ = float(input("Subtract: "))

            if num_ != 0:

                num -= num_

                continue

            else:

                break

                continue

        elif op == "mul":

            operation = "mul"

            num_ = float(input("Multiply: "))

            if num_ != 0:

                num *= num_

                continue

            else:

                break

                continue

        elif op == "div":

            operation = "div"

            num_ = float(input("Divide: "))

            if num_ != 0:

                num /= num_

                continue

            else:

                break

This seems to also reproduce the error when you input an operation not listed valid. The fix for this would be to break from the code and instead of putting in continue you would change it with break.

The fix:

valid = (None, "add", "sub", "mul", "div")
num = 0

operation = "add"

print("Operations: add(addition), sub(subtraction), mul(multiplication), div(division), adv(advanced)")

op = None

while True:

    print(num)

    op = str(input("Math operation: "))

    while True:

        print(num)

        if op not in valid:

            print("invalid operation!")

            op = None
            #the break
            break
        if op == "add":

            operation = "add"

            num_ = float(input("Add: "))

            if num_ != 0:

                num += num_

                continue

            else:

                break

                continue

        elif op == "sub":

            operation = "sub"

            num_ = float(input("Subtract: "))

            if num_ != 0:

                num -= num_

                continue

            else:

                break

                continue

        elif op == "mul":

            operation = "mul"

            num_ = float(input("Multiply: "))

            if num_ != 0:

                num *= num_

                continue

            else:

                break

                continue

        elif op == "div":

            operation = "div"

            num_ = float(input("Divide: "))

            if num_ != 0:

                num /= num_

                continue

            else:

                break

The output:

Operations: add(addition), sub(subtraction), mul(multiplication), div(division), adv(advanced)
0
Math operation: no
0
invalid operation!
0
Math operation: no
0
invalid operation!
0
Math operation:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Blue Robin