尝试在 VS Code 中执行 exit() 时出现错误消息
Error message when trying to execute exit() in VS Code
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()
当我尝试在 Microsoft VS Code 中打开我的脚本时,收到此错误消息:
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
当我在文件资源管理器中打开它时,脚本会在 Cmd 中自行关闭。但在 VS Code 中它不会。这是为什么?
在这种情况下,即使中断也可以停止脚本。
你在一个循环中。
您可以不使用 try except 语句退出。试试这个:
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()
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()
当我尝试在 Microsoft VS Code 中打开我的脚本时,收到此错误消息:
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
当我在文件资源管理器中打开它时,脚本会在 Cmd 中自行关闭。但在 VS Code 中它不会。这是为什么?
在这种情况下,即使中断也可以停止脚本。 你在一个循环中。
您可以不使用 try except 语句退出。试试这个:
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()