Python 错误类型错误 : unsupported operand type(s) for +: 'function' and 'function'

标签 python typeerror

我知道网上有一些关于此错误的资料,但无论我尝试什么,似乎都无法解决问题。我昨天才开始学习 Python,绝对是初学者,所以请不要评判我的脚本。这只是一个简单的脚本,想要从餐厅获取顾客的订单并计算这顿饭的总价。这很俗气,但我会喜欢一些帮助。我无法让我的脚本来计算这顿饭的总费用,无论我尝试什么,它都是行不通的。你能告诉我我做错了什么以及如何根据客户的选择来计算这顿饭的总费用吗?此外,如果他们选择菜单上没有的项目,程序将关闭而不是让他们重试。为什么?非常感谢您的帮助。 :) 谢谢

这是我在终端中运行脚本时遇到的成本错误的图片。 Error1

这是我输入菜单上没有的内容时得到的图片。 Error2

这是我的脚本。

请注意,我添加了菜单中的项目只是为了向您展示它们是什么以及它们的成本。

    Apple = 3
    Banana = 4
    Kiwi = 2
    Peach = 5
    Hamburger = 12
    Parma = 22
    Steak = 24
    Sandwhich = 10
    Cream = 3
    Cake = 8
    Moose = 2
    Soda = 3
    Beer = 8
    Wine = 12


    def Fruit():
        print("Welcome to The Buttler's Pantery")
        Fruit = raw_input("what fruit would you like today?")

        if (Fruit == "Apple"):
            Main()
        elif (Fruit == "Banana"):
            Main()
        elif (Fruit == "Kiwi"):
            Main()
        elif (Fruit == "Peach"):
            Main()
        else:
            print("Sorry, but it appears that the item you have ordered is not on the menu")

    def Main():
        Main = raw_input("what Main would you like today?")

        if (Main == "Hamburger"):
            Dessert()
        elif (Main == "Parma"):
            Dessert()
        elif (Main == "Steak"):
            Dessert()
        elif (Main == "Sandwhich"):
            Dessert()
        else:
            print("Sorry, but it appears that the item you have ordered is not on the menu")


    def Dessert():
        Dessert = raw_input("what Dessert would you like today?")

        if (Dessert == "Cream"):
            Beverage()
        elif (Dessert == "Cake"):
            Beverage()
        elif (Dessert == "Moose"):
            Beverage()
        else:
            print("Sorry, but it appears that the item you have ordered is not on the menu")

    def Beverage():
        Beverage = raw_input("what Beverage would you like today?")

        if (Beverage == "Soda"):
            print(add(num1, num2, num3, num4))
        elif (Beverage == "Beer"):
            print(add(num1, num2, num3, num4))
        elif (Beverage == "Wine"):
            print(add(num1, num2, num3, num4))

        else:
            print("Sorry, but it appears that the item you have ordered is not on the menu")

    def add(num1, num2, num3, num4):
        return num1 + num2 + num3 + num4

    def num1():
        if (Fruit == "Apple"):
            num1 = 3
        elif (Fruit == "Banana"):
            num1 = 4    
        elif (Fruit == "Kiwi"):
            num1 = 2    
        elif (Fruit == "Peach"):
            num1 = 5
        else: num1 = 0

    def num2():
        if (Main == "Hamburger"):
            num2 = 12
        elif (Main == "Parma"):
            num2 = 22
        elif (Main == "Steak"):
            num2 = 24
        elif (Main == "Sandwhich"):
            num2 = 10
        else: num2 = 0

    def num3():
        if (Dessert == "Cream"):
            num3 = 3
        elif (Dessert == "Cake"):
            num3 = 8
        elif (Dessert == "Moose"):
            num3 = 2
        else: num3 = 0

    def num4():
        if (Beverage == "Soda"):
            num4 = 3
        elif (Beverage == "Beer"):
            num4 = 8
        elif (Beverage == "Wine"):
            num4 = 12   
        else: num4 = 0









    Fruit()

最佳答案

你正在用你的局部变量名重新定义你的函数名

难怪为什么会有混淆。

我知道 visual basic 用于返回值,但你不能在 python 中这样做。

只需重命名您的局部变量(分配给 raw_input 结果的变量就可以了

关于Python 错误类型错误 : unsupported operand type(s) for +: 'function' and 'function' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39314935/

相关文章:

python - 在 Python 中使用 Beautiful Soup 在线检查产品的可用性

python - 在 Tkinter 中设置图标

javascript - 静态只读成员分配 => TypeError : XXX is not a constructor

javascript - 未捕获的类型错误 : Cannot read properties of undefined (reading 'remove' )

python - 为什么 Python 不可变类型(如 int、str 或元组)需要使用 `__new__()` 而不仅仅是 `__init__()` ?

python - 我是否应该在使用 numpy.array 时使用 numpy.float64 而不是 Python float

python - 在python中找到两个数据帧之间的差异(设置差异)

javascript - ReactJS TypeError : _this3. state.Objects.map 不是函数

python - TensorFlow TypeError : Fetch argument None has invalid type <type 'NoneType' >?

python - 为什么我在 python 中收到对象类型 int 的 TypeError?