Python 计算器有问题
Python Calculator having issues
所以我添加了一个功能来高效地写百分比,你所要做的就是输入一个数字,比如 6,然后按百分比,计算器会自动写出 0.06。当您开始为计算器输入表达式时一切正常:
但是当你按下等号时,事情就变糟了:
现在因为我的数学很糟糕,我真的认为这是正确的答案,所以现在我终于可以继续做其他事情了,对吧?但是我google了一下,上面说6*0.06是0.36,不是36.
这里事情又变得有点疯狂了,当我做 6/0.06 时,我得到 1,而不是 100,所以这意味着每当我除法时,我得到的结果比实际答案低 100 倍,每当我乘法时,我得到的东西比我要求的高 100 倍。
这是百分比函数:
def percent():
global expression
last_num = expression[-1]
percent = int(last_num)/100
equation.set(f"{expression[:-1]}{percent}")
如有任何帮助,我们将不胜感激!
仅对最后一位进行运算不适用于超过 1 位的数字。以下是我在 percent
函数中所做的修改,以使逻辑正常工作。此外,使用 eval
函数计算表达式,为“6*6”、“6/6”和“5/70”等输入提供所需的结果。
from tkinter import *
import string
window = Tk()
entry1string = StringVar()
entry_1 = Entry(window,textvariable=entry1string)
entry_1.pack()
def percent():
global expression
expression = entry1string.get()
#instead of last_num look for entire number e.g. in "60%", "60"
#so first find the operator which is used in the expression ( /, *, ...etc.)
operator = next((ele for ele in expression if ele in string.punctuation), None)
#Now get the number from the last whose % we need to calculate first
percent_num = expression.split(operator)[-1] #from the last everything after that '/' or '*' operator
#Also get the entire rest of the expression
#i.e. get the number before that operator along with the operator
rest_exp = expression.split(operator)[0]+operator
#print(percent_num)
percent = int(percent_num)/100
exprsn = f"{rest_exp}{percent}"
print(exprsn)
ans = eval(exprsn)
Label(window, text=ans).pack()
button1 = Button(window, text="%", command=percent)
button1.pack()
window.mainloop()
所以我添加了一个功能来高效地写百分比,你所要做的就是输入一个数字,比如 6,然后按百分比,计算器会自动写出 0.06。当您开始为计算器输入表达式时一切正常:
但是当你按下等号时,事情就变糟了:
现在因为我的数学很糟糕,我真的认为这是正确的答案,所以现在我终于可以继续做其他事情了,对吧?但是我google了一下,上面说6*0.06是0.36,不是36.
这里事情又变得有点疯狂了,当我做 6/0.06 时,我得到 1,而不是 100,所以这意味着每当我除法时,我得到的结果比实际答案低 100 倍,每当我乘法时,我得到的东西比我要求的高 100 倍。
这是百分比函数:
def percent():
global expression
last_num = expression[-1]
percent = int(last_num)/100
equation.set(f"{expression[:-1]}{percent}")
如有任何帮助,我们将不胜感激!
仅对最后一位进行运算不适用于超过 1 位的数字。以下是我在 percent
函数中所做的修改,以使逻辑正常工作。此外,使用 eval
函数计算表达式,为“6*6”、“6/6”和“5/70”等输入提供所需的结果。
from tkinter import *
import string
window = Tk()
entry1string = StringVar()
entry_1 = Entry(window,textvariable=entry1string)
entry_1.pack()
def percent():
global expression
expression = entry1string.get()
#instead of last_num look for entire number e.g. in "60%", "60"
#so first find the operator which is used in the expression ( /, *, ...etc.)
operator = next((ele for ele in expression if ele in string.punctuation), None)
#Now get the number from the last whose % we need to calculate first
percent_num = expression.split(operator)[-1] #from the last everything after that '/' or '*' operator
#Also get the entire rest of the expression
#i.e. get the number before that operator along with the operator
rest_exp = expression.split(operator)[0]+operator
#print(percent_num)
percent = int(percent_num)/100
exprsn = f"{rest_exp}{percent}"
print(exprsn)
ans = eval(exprsn)
Label(window, text=ans).pack()
button1 = Button(window, text="%", command=percent)
button1.pack()
window.mainloop()