带历史记录的Python计算器输出None的问题解决
解决Python计算器计算后输出多余None的问题
开发了一款支持历史记录的简易Python计算器,但每次执行计算操作后,控制台都会额外输出“None”,以下是原代码及运行输出示例:
原代码
calculations = [] def record_history(args): calculations.append(args) def history(): if calculations == []: print("No past calculations to show") else: for i in calculations: print(i) def add(a,b): return a+b def subtract(a,b): return a-b def multiply (a,b): return a *b def divide(a,b): try: return a/b except Exception as e: print(e) def power(a,b): return a**b def remainder(a,b): return a % b def select_op(choice): if choice == '#': return -1 elif (choice == '$'): return 0 elif (choice in ('+','-','*','/','^','%')): while (True): num1s = (input("Enter first number: ")) print(num1s) if num1s.endswith('$'): return 0 if num1s.endswith('#'): return -1 try: num1 = float(num1s) break except: print("Not a valid number,please enter again") continue while (True): num2s = str(input("Enter second number: ")) print(num2s) if num2s.endswith('$'): return 0 if num2s.endswith('#'): return -1 try: num2 = float(num2s) break except: print("Not a valid number,please enter again") continue last_calculation = "" if choice == '+': result = add(num1, num2) elif choice == '-': result = subtract(num1, num2) elif choice == '*': result = multiply(num1, num2) elif choice == '/': result = divide(num1, num2) elif choice == '^': result = power(num1, num2) elif choice == '%': result = remainder(num1, num2) else: print("Something Went Wrong") last_calculation = "{0} {1} {2} = {3}".format(num1, choice, num2, result) print((last_calculation )) print((record_history(last_calculation))) # 问题所在 elif choice == "?": history() else: print("Unrecognized operation") while True: print("Select operation.") print("1.Add : + ") print("2.Subtract : - ") print("3.Multiply : * ") print("4.Divide : / ") print("5.Power : ^ ") print("6.Remainder: % ") print("7.Terminate: # ") print("8.Reset : $ ") print("8.History : ? ") # take input from the user choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ") print(choice) if(select_op(choice) == -1): #program ends here print("Done. Terminating") exit()
原运行输出示例
Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): ? No past calculations to show Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): - Enter first number: 2 Enter second number: 3 2.0 - 3.0 = -1.0 None # 多余的None输出 Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): ? 2.0 - 3.0 = -1.0 Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): # Done. Terminating
问题原因
record_history函数仅执行添加记录的操作,没有定义return语句,Python中函数默认返回None。代码中用print(record_history(last_calculation))调用该函数时,会把函数的返回值(也就是None)打印出来,这就是多余输出的来源。
解决方案
直接调用record_history(last_calculation),不需要用print包裹它。修改后的关键代码片段如下:
last_calculation = "{0} {1} {2} = {3}".format(num1, choice, num2, result) print(last_calculation) record_history(last_calculation) # 移除外层的print
修改后的完整代码
calculations = [] def record_history(args): calculations.append(args) def history(): if calculations == []: print("No past calculations to show") else: for i in calculations: print(i) def add(a,b): return a+b def subtract(a,b): return a-b def multiply (a,b): return a *b def divide(a,b): try: return a/b except Exception as e: print(e) def power(a,b): return a**b def remainder(a,b): return a % b def select_op(choice): if choice == '#': return -1 elif (choice == '$'): return 0 elif (choice in ('+','-','*','/','^','%')): while (True): num1s = (input("Enter first number: ")) print(num1s) if num1s.endswith('$'): return 0 if num1s.endswith('#'): return -1 try: num1 = float(num1s) break except: print("Not a valid number,please enter again") continue while (True): num2s = str(input("Enter second number: ")) print(num2s) if num2s.endswith('$'): return 0 if num2s.endswith('#'): return -1 try: num2 = float(num2s) break except: print("Not a valid number,please enter again") continue last_calculation = "" if choice == '+': result = add(num1, num2) elif choice == '-': result = subtract(num1, num2) elif choice == '*': result = multiply(num1, num2) elif choice == '/': result = divide(num1, num2) elif choice == '^': result = power(num1, num2) elif choice == '%': result = remainder(num1, num2) else: print("Something Went Wrong") last_calculation = "{0} {1} {2} = {3}".format(num1, choice, num2, result) print(last_calculation) record_history(last_calculation) # 已修改:移除print elif choice == "?": history() else: print("Unrecognized operation") while True: print("Select operation.") print("1.Add : + ") print("2.Subtract : - ") print("3.Multiply : * ") print("4.Divide : / ") print("5.Power : ^ ") print("6.Remainder: % ") print("7.Terminate: # ") print("8.Reset : $ ") print("8.History : ? ") # take input from the user choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ") print(choice) if(select_op(choice) == -1): #program ends here print("Done. Terminating") exit()
修改后的运行输出示例
Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): ? No past calculations to show Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): - Enter first number: 2 Enter second number: 3 2.0 - 3.0 = -1.0 # 不再输出None Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): ? 2.0 - 3.0 = -1.0 Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): # Done. Terminating
内容的提问来源于stack exchange,提问作者dharsh
相关产品推荐
相关产品推荐

