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函數和索引有關嗎?爲什麼我的計算器不能計算答案?
請不要進行代碼轉儲,嘗試隔離代碼似乎在做的事情,而不是預期的部分。 –
爲什麼這肯定意味着你可以運行該程序並親自查看?我只是想幫忙! –
這幾乎肯定不是你的錯誤的原因,但只是一個建議:最常見的Python風格是對模塊,函數和變量使用'lower_case_names_with_underscores',並使用CapitolizedNamesWithoutUnderscores作爲類。遵循這個約定會讓其他人更容易閱讀你的代碼。並非所有的標準庫模塊都遵循本指南(有文檔記載,以及[PEP 8](https://www.python.org/dev/peps/pep-0008/)中的許多其他樣式建議,但建議對於新代碼 – Blckknght