Keep receiving:TypeError:python中不支持*:“NoneType”和“int”的操作数类型

2024-05-16 01:59:13 发布

您现在位置: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()

我希望能够选择食物的种类和具体价格,当你输入你需要购买的数量时,它会乘以价格并给出答案


Tags: oftheyoureturnfoodhaveenhancedword
1条回答
网友
1楼 · 发布于 2024-05-16 01:59:13

我可以在你的代码中找出一些错误,首先“==”是比较两个值的操作符,“=”是赋值的操作符。因此,对于“foodType()==something”,您没有分配任何内容,它将返回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()是一个函数,尽管它不应该是。你知道吗

即使你没有把它变成一个函数,但有些东西并不能完全相加——你在'a'中给foodType赋值一个字符串,如果你把它乘以num1,它也不会返回价格,但是字符串num1乘以如下:

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

那可能不是你想要的。你知道吗

有什么不清楚的尽管问。你知道吗

相关问题 更多 >