Home > Mobile >  Error message when trying to execute exit() in VS Code
Error message when trying to execute exit() in VS Code

Time:05-20

def calculate():
    while True:
            operator = input("What operator do you wanna use(*,/, ,-)? ")
            possible_op = " -*/"

            if operator not in possible_op:
                continue
            try:
                number_1 = float(input("What is your first number? "))
                number_2 = float(input("What is your second number? "))
            except ValueError:
                continue
        
            if operator == " ":
                print(number_1   number_2) 
            elif operator == "-":
                print(number_1 - number_2) 
            elif operator == "*":
                print(number_1 *  number_2) 
            elif operator == "/":
                print(number_1 / number_2) 


            try:
                print("Do you wanna calculate again? ")
                answer = input("(Y/N) ").lower()
                possible_answers = ["y", "n"]
                if answer == "y":
                    return calculate()
                elif answer == "n":
                    exit()
            except input != possible_answers: 
                print("Wrong Input")
                continue 

calculate()

When I tried to open my script in Microsoft VS Code, I got this error message:

Traceback (most recent call last):
  File "e:\Python\Projects\VsCode\calculator\calculator_school.py", line 31, in calculate
    exit()
  File "D:\Schule\Phyton\lib\_sitebuiltins.py", line 26, in __call__
    raise SystemExit(code)
SystemExit: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:\Python\Projects\VsCode\calculator\calculator_school.py", line 36, in <module>
    calculate()
  File "e:\Python\Projects\VsCode\calculator\calculator_school.py", line 32, in calculate
    except input != possible_answers:
TypeError: catching classes that do not inherit from BaseException is not allowed

When I open it in my file explorer, the script closes itself in Cmd as it should. But in VS Code it just won't. Why is that?

CodePudding user response:

You can stop the script even with a break in this case. you're inside a single loop.

CodePudding user response:

You can exit without using try except statement. Try this:

def calculate():
  while True:
    operator = input("What operator do you wanna use(*,/, ,-)? ")
    possible_op = " -*/"

    if operator not in possible_op:
      continue
    try:
      number_1 = float(input("What is your first number? "))
      number_2 = float(input("What is your second number? "))
    except ValueError:
      continue

    if operator == " ":
      print(number_1   number_2) 
    elif operator == "-":
      print(number_1 - number_2) 
    elif operator == "*":
      print(number_1 *  number_2) 
    elif operator == "/":
      print(number_1 / number_2) 
    while True:
      print("Do you wanna calculate again? ")
      answer = input("(Y/N) ").lower()
      if answer == "y":
        return calculate()
      elif answer == "n":
        quit()
      else: 
        print("Wrong Input")
        continue 
calculate()
  • Related