簡體   English   中英

在 Python 中使用浮點值索引列表的 TypeError

[英]TypeError for indexing list with float values in Python

我正在嘗試使用 Tkinter 創建一個簡單的計算器。 為此,我創建了一個列表來存儲我想要操作的數字,以便我可以使用該列表進行加/減/乘/除。 但是,當我嘗試訪問該列表時,我收到一個 TypeError,即使我已將列表中的值轉換為浮點數。 這是我不斷收到的錯誤:第 37 行,在計算中

result = operators[current_operator(current_input[0], current_input[1])]
TypeError: 'str' object is not callable

這是我的代碼:

import tkinter as tk
from tkinter import messagebox

# take number function
def take_number(event):
    global current_input
    global input_box
    global current_number
    current_number += event.widget["text"]
    input_box.insert(10, current_number)



# take operator event and append current number to number list
def take_operator(event):
    global current_input
    global current_number
    global current_operator
    current_operator = event.widget["text"]
    input_box.insert(10, current_operator)
    current_input.append(float(current_number))
    current_number = ""




# calculate by appending current number to number list and using the list to get first and second number
def calculate():
    global current_input
    global current_operator
    global input_box
    current_input.append(float(current_number))
    input_box.delete(0, 10)
    result = operators[current_operator(current_input[0], current_input[1])]
    input_box.insert(10, result)


# create window
window = tk.Tk()
window.title("Pocket calculator")
current_input = []
current_number = ""
current_operator = ""
# create a dictionary to link to operators
operators = {"+": lambda x, y: x + y,
             "-": lambda x, y: x - y,
             "*": lambda x, y: x * y,
             "/": lambda x, y: x / y}

# create enter box
input_box = tk.Entry(window, text="0", width=10)
input_box.grid(column=0, row=0)

# create buttons
number_0 = tk.Button(window, text="0")
number_0.grid(column=0, row=4)
number_0.bind("<Button-1>", take_number)

for i in range(3):
    one_to_three = tk.Button(window, text=str(i+1))
    one_to_three.grid(column=i, row=3)
    one_to_three.bind("<Button-1>", take_number)

for i in range(3, 6):
    four_to_six = tk.Button(window, text=str(i+1))
    four_to_six.grid(column=i % 3, row=2)
    four_to_six.bind("<Button-1>", take_number)

for i in range(6, 9):
    seven_to_nine = tk.Button(window, text=str(i+1))
    seven_to_nine.grid(column=i % 3, row=1)
    seven_to_nine.bind("<Button-1>", take_number)

c_button = tk.Button(window, text="C")
c_button.grid(column=1, row=4)

dot_button = tk.Button(window, text=".")
dot_button.grid(column=2, row=4)
dot_button.bind("<Button-1>", take_number)

equal_button = tk.Button(window, text="=", command=calculate)
equal_button.grid(column=3, row=3)


sign_button = tk.Button(window, text="+/-")
sign_button.grid(column=3, row=4)

plus_button = tk.Button(window, text="+")
plus_button.grid(column=4, row=1)
plus_button.bind("<Button-1>", take_operator)

minus_button = tk.Button(window, text="-")
minus_button.grid(column=4, row=2)
minus_button.bind("<Button-1>", take_operator)

multiply_button = tk.Button(window, text="*")
multiply_button.grid(column=4, row=3)
multiply_button.bind("<Button-1>", take_operator)

division_button = tk.Button(window, text="/")
division_button.grid(column=4, row=4)
division_button.bind("<Button-1>", take_operator)


# start application
window.mainloop()

operators[current_operator(current_input[0], current_input[1])]

operators是一個帶有str鍵和lambda值的dictionary

您正在調用current_operator(..)但您需要調用operators[current_operator](..)

所以將您的行更改為:

operators[current_operator](current_input[0], current_input[1])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM