故意犯错后如何停止回溯调用?
How to stop a traceback call after making a deliberate mistake?
我正在制作一个计算器,它只执行一定数量的操作,如果输入任何其他内容,它会打印一条提醒消息。
但是在那个过程中,就在它打印'a' 之后发生了名称错误。
我哪里错了?
if Operation == '+':
c = a + b
elif Operation == '-':
c = a - b
elif Operation == '*':
c = a * b
elif Operation == '/':
c = a / b
else:
print('Read the Instructions again, dimmwit')
print('Your answer is', c)
print('Thanks! Have a great time!')
请就我应该如何改进我的代码提出一些建议。
因为这个:
else:
print('Read the Instructions again, dimmwit')
有可能到时候
print('Your answer is', c)
到达 ,如果采用 else
路线,则没有 c
定义。
如果你把这个做成一个函数,比如:
def print_result(operation, a, b):
if operation == '+':
c = a + b
elif operation == '-':
c = a - b
elif operation == '*':
c = a * b
elif operation == '/':
c = a / b
else:
print('Read the Instructions again, dimmwit')
return
print('Your answer is', c)
print('Thanks! Have a great time!')
然后您可以提前停止使用 return
,而不是尝试打印一个从未计算过的答案 c
。
您确实还应该 post 您得到的特定 NameError 和回溯。 (以及您的完整代码。)
无论如何,问题是你没有将 c
初始化为任何东西,所以当控制超出“侮辱用户”else
时,尝试打印 c
可以不会发生。
在所有情况下都给c
一些值,然后检查你是否真的做了一个操作:
c = None
if Operation == "+":
c = a + b
elif Operation == "-":
c = a - b
elif Operation == "*":
c = a * b
elif Operation == "/":
c = a / b
else:
print("Read the Instructions again, dimmwit")
if c is not None:
print("Your answer is", c)
print("Thanks! Have a great time!")
更好的是,在它自己的函数中进行计算,并在其他地方向用户输出内容:
def compute(operation, a, b):
if operation == "+":
return a + b
elif operation == "-":
return a - b
elif operation == "*":
return a * b
elif operation == "/":
return a / b
return None
c = compute(operation, a, b)
if c is not None:
print("Your answer is", c)
print("Thanks! Have a great time!")
else:
print("Please read the instructions again.")
我正在制作一个计算器,它只执行一定数量的操作,如果输入任何其他内容,它会打印一条提醒消息。 但是在那个过程中,就在它打印'a' 之后发生了名称错误。 我哪里错了?
if Operation == '+':
c = a + b
elif Operation == '-':
c = a - b
elif Operation == '*':
c = a * b
elif Operation == '/':
c = a / b
else:
print('Read the Instructions again, dimmwit')
print('Your answer is', c)
print('Thanks! Have a great time!')
请就我应该如何改进我的代码提出一些建议。
因为这个:
else:
print('Read the Instructions again, dimmwit')
有可能到时候
print('Your answer is', c)
到达 ,如果采用 else
路线,则没有 c
定义。
如果你把这个做成一个函数,比如:
def print_result(operation, a, b):
if operation == '+':
c = a + b
elif operation == '-':
c = a - b
elif operation == '*':
c = a * b
elif operation == '/':
c = a / b
else:
print('Read the Instructions again, dimmwit')
return
print('Your answer is', c)
print('Thanks! Have a great time!')
然后您可以提前停止使用 return
,而不是尝试打印一个从未计算过的答案 c
。
您确实还应该 post 您得到的特定 NameError 和回溯。 (以及您的完整代码。)
无论如何,问题是你没有将 c
初始化为任何东西,所以当控制超出“侮辱用户”else
时,尝试打印 c
可以不会发生。
在所有情况下都给c
一些值,然后检查你是否真的做了一个操作:
c = None
if Operation == "+":
c = a + b
elif Operation == "-":
c = a - b
elif Operation == "*":
c = a * b
elif Operation == "/":
c = a / b
else:
print("Read the Instructions again, dimmwit")
if c is not None:
print("Your answer is", c)
print("Thanks! Have a great time!")
更好的是,在它自己的函数中进行计算,并在其他地方向用户输出内容:
def compute(operation, a, b):
if operation == "+":
return a + b
elif operation == "-":
return a - b
elif operation == "*":
return a * b
elif operation == "/":
return a / b
return None
c = compute(operation, a, b)
if c is not None:
print("Your answer is", c)
print("Thanks! Have a great time!")
else:
print("Please read the instructions again.")