我使用python编写代码将历史结果保存在计算器中



我编写了使用python存储历史结果的代码,此代码工作正常。但是我想显示历史和下面的图片一样。在这个程序中只显示历史记录的输出值。但是我需要整个过程的计算历史,而不仅仅是最后的输出值。请帮帮我。

问题图片请参见问题

请看看这个图像,我想把它作为预期的输出。

last_calculation = []
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  : ? ")

def add(x,y):
return x+y
def substract(x,y):
return x-y
def multiply(x,y):
return x*y
def divide(x,y):
return x/y
def power(x,y):
return x**y
def remainder(x,y):
return x%y
def history():
global last_calculation
if last_calculation == []:
print("No past calculations to show")
else:
for i in last_calculation:
print(i)  #for single line


# take input from the user
choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ")
print(choice)
if choice == "#":
#program ends here
print("Done. Terminating")
exit()
if choice == "$":
continue
if choice=='?':
history()
continue
try:
a=input("Enter first number: ")
print(a)
if a == "#":
#program ends here
print("Done. Terminating")
exit()
a=float(a)

b=input("Enter second number: ")
print(b)
if b == "#":
#program ends here
print("Done. Terminating")
exit()
b=float(b)

except ValueError:
continue

if choice == "+":
result = add(a, b)
last_calculation.append(result)
print(a, '+', b, '=', add(a,b))

elif choice == "-":
result = substract(a, b)
last_calculation.append(result)
print(a, "-", b, "=", substract(a,b))
elif choice == "*":
result = multiply(a, b)
last_calculation.append(result)
print(a, "*", b, "=", multiply(a,b))
elif choice == "/":
if b == 0.0:
last_calculation.append(result)
print("float division by zero")
print(a, "/", b, "=" " None")
else:
result =  divide(a, b)
last_calculation.append(result)
print(a, "/", b, "=", divide(a,b)) 

elif choice == '^':
result = power(a, b)
last_calculation.append(result)
print(a, "^", b, "=", power(a,b))
elif choice == "%":
result = remainder(a, b)
last_calculation.append(result)
print(a, "%", b, "=", remainder(a,b))
else :
print("Unrecognized operation")

关于如何提高代码的可读性以及问题的根本原因,我有一些小提示。你的代码中有几个部分是可以工作的,但是比它们需要的稍微难以阅读。

  1. 我不确定每个操作有单独的功能是什么。由于它们都是一行操作,并且都只在代码的另一个地方被调用,因此您现在可以简单地将函数体放在调用它们的地方。

  2. 与其使用长if-elif-else结构,不如使用match-case语句。(它们在其他语言中被称为switch-case语句。请看这篇文章

  3. 使用While True通常被认为是不好的做法。在你的while循环定义中有一个退出条件更容易读,对于更复杂的项目更容易维护

你的问题的原因是,在你的代码中,last_calculation只被append-ed与你的计算结果,而不是你正在显示的实际字符串。请看下面修改后的代码:

last_calculation = []
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  : ? ")

def add(x,y):
return x+y
def substract(x,y):
return x-y
def multiply(x,y):
return x*y
def divide(x,y):
return x/y
def power(x,y):
return x**y
def remainder(x,y):
return x%y
def history():
global last_calculation
if last_calculation == []:
print("No past calculations to show")
else:
for i in last_calculation:
print(i)  #for single line


# take input from the user
choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ")
print(choice)
if choice == "#":
#program ends here
print("Done. Terminating")
exit()
if choice == "$":
continue
if choice=='?':
history()
continue
try:
a=input("Enter first number: ")
print(a)
if a == "#":
#program ends here
print("Done. Terminating")
exit()
a=float(a)

b=input("Enter second number: ")
print(b)
if b == "#":
#program ends here
print("Done. Terminating")
exit()
b=float(b)

except ValueError:
continue

if choice == "+":
resultStr = a + '+' + b + '=' + add(a,b)
last_calculation.append(resultStr)
print(resultStr)

elif choice == "-":
resultStr = a + '-' + b + '=' + substract(a,b)
last_calculation.append(resultStr)
print(resultStr)
elif choice == "*":
resultStr = a + '*' + b + '=' + multiply(a,b)
last_calculation.append(resultStr)
print(resultStr)
elif choice == "/":
if b == 0.0:
resultStr = a + '/' + b + '=' + 'none'
last_calculation.append(resultStr)
print(resultStr)
else:
resultStr = a + '/' + b + '=' + divide(a,b)
last_calculation.append(resultStr)
print(resultStr)

elif choice == '^':
resultStr = a + '^' + b + '=' + power(a,b)
last_calculation.append(resultStr)
print(resultStr)

elif choice == "%":
resultStr = a + '%' + b + '=' + remainder(a,b)
last_calculation.append(resultStr)
print(resultStr)
else :
print("Unrecognized operation")

加上我建议的改动:


last_calculation = []
choice = "Doesn't matter what I put here"
while choice != "#":
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  : ? ")

def history():
global last_calculation
if last_calculation == []:
print("No past calculations to show")
else:
for i in last_calculation:
print(i)  #for single line


# take input from the user
choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ")
print(choice)
match choice:
case "#":
#program ends here
print("Done. Terminating")
continue
case "$":
continue
case '?':
history()
continue

case _:
try:
a=input("Enter first number: ")
print(a)
if a == "#":
#program ends here
print("Done. Terminating")
continue
a=float(a)

b=input("Enter second number: ")
print(b)
if b == "#":
#program ends here
print("Done. Terminating")
continue
b=float(b)
except ValueError:
continue

match choice:
case  "+":
resultStr = a + '+' + b + '=' + (a+b)
last_calculation.append(resultStr)
print(resultStr)

case "-":
resultStr = a + '-' + b + '=' + (a-b)
last_calculation.append(resultStr)
print(resultStr)
case "*":
resultStr = a + '*' + b + '=' + (a*b)
last_calculation.append(resultStr)
print(resultStr)
case "/":
if b == 0.0:
resultStr = a + '/' + b + '=' + 'none'
last_calculation.append(resultStr)
print(resultStr)
else:
resultStr = a + '/' + b + '=' + (a/b)
last_calculation.append(resultStr)
print(resultStr)

case '^':
resultStr = a + '^' + b + '=' + (a**b)
last_calculation.append(resultStr)
print(resultStr)

case "%":
resultStr = a + '%' + b + '=' + (a%b)
last_calculation.append(resultStr)
print(resultStr)
case  _:
print("Unrecognized operation")

相关内容

  • 没有找到相关文章

最新更新