2015-01-06 117 views
-4
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'时,该条目返回'错误'而不是'10'的消息!为什么是这样? 它可能是与addFunction函数和索引有关吗?为什么我的计算器不能计算答案?

+3

请不要进行代码转储,尝试隔离代码似乎在做的事情,而不是预期的部分。 –

+0

为什么这肯定意味着你可以运行该程序并亲自查看?我只是想帮忙! –

+1

这几乎肯定不是你的错误的原因,但只是一个建议:最常见的Python风格是对模块,函数和变量使用'lower_case_names_with_underscores',并使用CapitolizedNamesWithoutUnderscores作为类。遵循这个约定会让其他人更容易阅读你的代码。并非所有的标准库模块都遵循本指南(有文档记载,以及[PEP 8](https://www.python.org/dev/peps/pep-0008/)中的许多其他样式建议,但建议对于新代码 – Blckknght

回答

0

在您的代码:

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

调用addFunction返回一个字符串: '8 + 9'; 然后你试图将这个字符串转换为一个浮点数。 它引发了一个ValueError错误,cuz字符串不能转为浮动。

要解决这个问题,你需要去你调用addFunction,使它返回8 + 9,而不是“8 + 9”,

我觉得你的问题是在这条线:

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

您正在将add_selection [index]转换为float,然后转换为字符串。尝试删除外STR()

编辑:

进一步了解的唯一方法是打印检查这一行:

试着这样做:

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

EDIT2:

更好地看待您的代码有两件事可能会解决您的程序问题。

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是一个全局变量,真的不好意思,并可能导致几个问题的进一步。

+0

不出现同样的错误出现。我同意你的意见,这是导致错误 –

+0

打印检查该行的那一行。 –

+0

我不知道我可以放置哪条线 –