为什么我的计算器不能计算出答案?

2024-05-28 23:48:46 发布

您现在位置:Python中文网/ 问答频道 /正文

def Functional_Output(Validate_Input):
    try:
        while '(' in Validate_Input and ')' in Validate_Input:
            Validate_Output = Validate_Input.count('(')

            Intake_Input = Validate_Input.find('(')
            fin = Validate_Input.find(')')+1

            while Validate_Output > 1:
                Intake_Input = Validate_Input.find('(', Intake_Input+1)
                Validate_Output -= 1

            receive_value = Validate_Input[Intake_Input:fin]
            receive_input = calcula(receive_value.replace('(', ''). replace(')', ''))
            Validate_Input = Validate_Input.replace(recieve_value, recieve_input)

        DisplayAnswer = float(AddFunction(Validate_Input))

    except:
        DisplayAnswer = "Error"
    return DisplayAnswer
def AddFunction(Validate_Input):
    add_selection = Validate_Input.split()

    while len(add_selection) != 1:

        for index in range(len(add_selection)):
            if add_selection[index] == '/':
                add_selection[index] = str(float(add_selection[index-1]) / float(add_selection[index+1]))
                add_selection.pop(index+1)
                add_selection.pop(index-1)
                break
            elif add_selection[index] == '*':
                add_selection[index] = str(float(add_selection[index-1]) * float(add_selection[index+1]))
                add_selection.pop(index+1)
                add_selection.pop(index-1)
                break
        if not '/' in add_selection and not '*' in add_selection:
            while len(add_selection) !=1:
                for index in range(len(add_selection)):
                    add_selection[index] = str(float(add_selection[index]) + float(add_selection[index+1]))

                    add_selection.pop(index+1)
                    break
    return add_selection[0]

root = tkinter.Tk()
root.resizable(0, 0)
root.title("Calculator")
txtDisplay =tkinter.StringVar()
operator = ""
App_Function = Calculator_App(root)

root.mainloop()

为什么当我点击按钮“9”,然后是“+”然后是“1”时,条目返回的信息是“Error”而不是“10”!为什么会这样? 是否与addFunction函数和索引有关?你知道吗


Tags: inaddinputoutputindexlenrootfind
1条回答
网友
1楼 · 发布于 2024-05-28 23:48:46

在代码中:

line 98, in Functional_Output DisplayAnswer = float(AddFunction(Validate_Input))

AddFunction返回字符串:“8+9”; 然后您尝试将此字符串转换为浮点。 它抛出一个ValueError,因为字符串不能变成float。你知道吗

要解决这个问题,需要转到AddFunction并使其返回8+9而不是“8+9”

我认为你的问题在于:

add_selection[index] = str(float(add_selection[index]) + float(add_selection[index+1]))

您正在将add\ U selection[索引]转换为浮点,然后转换为字符串。尝试删除外部str()

编辑:

进一步了解的唯一方法是打印支票行:

尝试这样做:

print add_selection[index], add_selection[index+1]
print type(add_selection[index]),type(add_selection[index+1])

编辑2:

更好地审视你的代码有两件事可能会修复你的程序。你知道吗

1)轻松破解

AddFunction更改中:

return add_selection[0]

return eval(add_selection[0])

由于AddFunction返回的是'9+1' <type str>,因此使用eval将其计算为10 <type int>。实际上,您可以删除整个AddFunction,只需使用DisplayAnswer = float(eval(Validate_Input))。但请注意,这是一个dirty trick and a hack。你知道吗

2)重写

我注意到,您在add_Components中使用每个按钮,既可以显示信息(字符串),也可以作为按下每个按钮时的值,从而将运算符与新值连接起来。operator += self.value。你知道吗

但最主要的问题是,您处理的是字符串而不是整数,当您将按钮的值设置为'1'时,这与将其设置为1不同,您应该将程序更改为对值使用正确的类型,数字是整数,字符串是运算符。然后你必须重写你的AddFunction如何处理这两者。你知道吗

最后,你operator作为一个全局变量是非常不受欢迎的,并且会导致一些问题。你知道吗

相关问题 更多 >

    热门问题