继续接收:TypeError:*:Python中不支持的操作数类型:'NoneType'和'int'

时间:2019-04-25 09:08:53

标签: python

我正在尝试创建一个程序,该程序在您选择鸡肉后会增加成本,并不断给我“ NoneType”和“ int”的类型错误

我是Python的新手,不知道该怎么做

def printMenu():
    print("Choose your type of chook food: ")
    print("A. Display Chook Food Types")
    print("B. Calculate total cost of the selected chook food")
    print("C. Exit the program")
    word = input("Enter A, B, C or D to proceed: ")
    if word == "A":
        print ("Choose the type of chook food: ")
        print ("A. 10kg Pellets")
        print ("B. 50kg Pellets")
        print ("C. 10kg Mash")
        print ("D. 50kg Mash")
        print ("E. 10kg Enhanced")
        print ("F. 50kg Enhanced")
        print ("G. Return to menu")
        food = input("Enter A, B, C, D, E or F, G: ")

        if food == "A":
            foodType() == smallPellets
            print ("You have chosen 10kg Pellets")
            return printMenu()

        elif food == "B":
            foodType() == largePellets
            print ("You have chosen 50kg Pellets")
            return printMenu()

        elif food == "C":
            foodType() == smallMash
            print ("You have chosen 10kg Mash")
            return printMenu()

        elif food == "D":
            foodType() == largeMash
            print ("You have chosen 50kg Mash")
            return printMenu()

        elif food == "E":
            foodType() == smallEnhanced
            print ("You have chosen 10kg Enhanced")
            return printMenu()
        elif food == "F":
            foodType() == largeEnhanced
            print ("You have chosen 50kg Enhanced")
            return printMenu()

        elif food == "G":
            return printMenu()

    elif word == "B":
        num1 = eval(input("Enter the amount you want to purchase: "))
        foodType() * num1 == totalCost
        print(totalCost)


    elif word == "C":
        exit()
    else:
        print ("You have entered the wrong key, try A, B, C or D")
        print ("")
    return printMenu()

我希望能够选择食物的类型及其特定的价格,当您输入需要购买的数量时,价格会乘以价格并给出答案

1 个答案:

答案 0 :(得分:0)

我可以在您的代码中找出一些错误,首先“ ==”是比较两个值的运算符,“ =”是 assigning 值。因此,对于“ foodType()==某物”,您没有分配任何内容,它将返回true或False,这取决于foodType()是什么。

另外,当某物后面有方括号时,则表示它是一个函数,通常不能为该函数赋值,如果要保存多个订单,我会执行以下操作:

order = [] # create this list in the beginning of the function
# here come all your prints
if word == 'A':
    order.append("some type of food")
elif word == 'B':
    order.append("some type of food")
#...
else: # try to always include an else-statement for catching errors
    print("Food unknown")
# then, for price evaluation
totalPrice = 0
for i in range(len(order)):
    amount = int(input("How much", order[i], "do you want to buy?"))
    totalPrice += price(order[i]) * amount
    # price() is a function that returns the price of the given food type

这不是完整的代码,更多的是概述,您必须自己将其拼凑起来。您的错误可能是由totalCost = foodType() * num1引起的,因为正如已经指出的,foodType()是一个函数,即使它不应该这样。

即使您不将其设置为函数,也可能无法完全实现-您将字符串分配给foodType中的'A',并且将其乘以num1不会返回价格,但是字符串num1这样的时间:

>>> foodType = "pellets"
>>> num1 = 3
>>> print(foodType * num1)
>>> 'pelletspelletspellets'

那可能不是您想要的。

可以随时询问是否不清楚。