ValueError:“”不在列表中

时间:2019-04-26 04:13:43

标签: function user-interface programming-languages

输入常量列表中的值时出现错误(附照片),我无法弄清楚。

Exception in Tkinter callback
Traceback (most recent call last):
    File . "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
    File "/Users/jackmiles/Desktop/ProgrammingAssignment/ProgAsgStage2.py", line 58, in calculateCost
foodTypeIndex = FOOD_TYPE.index(foodSelected)
ValueError: '' is not in list

尝试一切都在别人的帮助下更改了名称,但仍然 没事

FOOD_TYPE = ["Pellets", "Mash", "Enhanced"]
WEIGHT = [10, 50]
PRICE = [[22.75, 100], [20.50, 90], [25.50, 125.50]]

def calCost():
    window3 = Tk()

    def calculateCost():
        foodSelected = TypeText.get()
        sizeSelected = TypeAmount.get()
        numberOfBags = TypeBagAmount.get()

        foodTypeIndex = FOOD_TYPE.index(foodSelected)
        weightIndex = WEIGHT.index(int(sizeSelected))
        quantity = int(numberOfBags)

        totalPrice = quantity * PRICE[foodTypeIndex][weightIndex]

        totalPriceLabel = Label(window3, text="$" + str(totalPrice))
        totalPriceLabel.grid(row=4, column=1, sticky=E)

    lsttext = Label(window3, font="Times 30", text="Please select chook 
    feed type Pellets, Mash or Enhanced:")
    lsttext.grid(row=0, column=0, sticky=E)

    TypeText = StringVar()
    entFood = Entry(window3, width=10, textvariable=TypeText)
    entFood.grid(row=0, column=1, sticky=W)

    lblbagsize = Label(window3, font="Times 30", text="Please enter 
    size of bags 10 or 50 Kilograms:")
    lblbagsize.grid(row=2, column=0, sticky=E)

    TypeAmount = StringVar()
    entAmount = Entry(window3, width=10, textvariable=TypeAmount)
    entAmount.grid(row=2, column=1, sticky=W)

    lblBagAmount = Label(window3, font="Times 30", text="Please 
    enter 
    amount of bags:")
    lblBagAmount.grid(row=3, column =0, sticky=E)

    TypeBagAmount = StringVar()
    entBagAmount = Entry(window3, width=10, 
    textvariable=TypeBagAmount)
    entBagAmount.grid(row=3, column=1, sticky=W)

    btnCalculate = Button(window3, font="Times 30", text="Calculate 
    Cost", command=calculateCost)
    btnCalculate.grid(sticky=E, row=4, column=0)

它应该在输入食物类型,大小和成本时进行计算。仅供参考,有更多运行该功能的代码,我刚刚发布了遇到问题的功能。感谢您的帮助:)

0 个答案:

没有答案