Python错误类型错误:不支持+:“function”和“function”的操作数类型

Python错误类型错误:不支持+:“function”和“function”的操作数类型,python,typeerror,Python,Typeerror,我知道网上有一些关于这个错误的东西,但不管我怎么做,似乎都无法修复它。我昨天刚开始学习Python,是一个绝对的初学者,所以请不要评判我的脚本。这只是一个简单的脚本,想要从一家餐厅接受一位顾客的订单,然后计算出这顿饭的总价格。这很俗气,但我想得到一些帮助。我不能让我的脚本来计算这顿饭的总成本,不管我怎么做,它就是不起作用。你能告诉我我做错了什么,以及如何根据顾客的选择计算餐费总额吗。此外,如果他们选择了菜单上没有的项目,程序将关闭,而不是让他们再试一次。为什么?我将非常感谢你的帮助谢谢 下面是我

我知道网上有一些关于这个错误的东西,但不管我怎么做,似乎都无法修复它。我昨天刚开始学习Python,是一个绝对的初学者,所以请不要评判我的脚本。这只是一个简单的脚本,想要从一家餐厅接受一位顾客的订单,然后计算出这顿饭的总价格。这很俗气,但我想得到一些帮助。我不能让我的脚本来计算这顿饭的总成本,不管我怎么做,它就是不起作用。你能告诉我我做错了什么,以及如何根据顾客的选择计算餐费总额吗。此外,如果他们选择了菜单上没有的项目,程序将关闭,而不是让他们再试一次。为什么?我将非常感谢你的帮助谢谢

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

这是一张如果我键入菜单上没有的内容时得到的图片。

这是我的剧本

请注意,我从菜单中列出了这些项目,只是为了告诉您它们是什么以及它们的成本

    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()

您正在使用局部变量名重新定义函数名

难怪会出现混乱

我知道VisualBasic用于返回值,但在python中不能这样做


只需将局部变量重命名为分配给原始输入结果的变量,就可以了

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

难怪会出现混乱

我知道VisualBasic用于返回值,但在python中不能这样做


只需将您的局部变量重命名为分配给原始输入结果的变量,就可以了

您不应该将函数命名为与变量相同的函数,即水果函数应如下所示:

def Fruit():
    print("Welcome to The Buttler's Pantery")
    global fruit
    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")
        Fruit()
还要注意全局性,它使水果在功能水果之外可以访问。如果您要将这些更改应用于所有函数,它应该可以工作

如果您还想让人们再试一次,您可以简单地再次调用该函数,就像在function Fruit中一样


希望这有帮助

您不应将函数命名为与变量相同的名称,即水果函数应如下所示:

def Fruit():
    print("Welcome to The Buttler's Pantery")
    global fruit
    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")
        Fruit()
还要注意全局性,它使水果在功能水果之外可以访问。如果您要将这些更改应用于所有函数,它应该可以工作

如果您还想让人们再试一次,您可以简单地再次调用该函数,就像在function Fruit中一样


希望这有帮助

您使用函数定义num1、num2。。。值,它将成为局部变量。而你应该把这些变量从函数中去掉。并使用关键字“global”在其他函数中使用这些变量

以下是编辑后的代码:

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
num1 = 0
num2 = 0
num3 = 0
num4 = 0


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

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

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

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


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

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

def Beverage():
    global num1
    global num2
    global num3
    global num4
    Beverage = raw_input("what Beverage would you like today?")

    if (Beverage == "Soda"):
        num4 = 3
        print(add(num1,num2,num3,num4))
    elif (Beverage == "Beer"):
        num4 = 8
        print(add(num1,num2,num3,num4))
    elif (Beverage == "Wine"):
        num4 = 12
        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(a,b,c,d) :
    return a+b+c+d



Fruit()

您使用函数定义num1、num2。。。值,它将成为局部变量。而你应该把这些变量从函数中去掉。并使用关键字“global”在其他函数中使用这些变量

以下是编辑后的代码:

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
num1 = 0
num2 = 0
num3 = 0
num4 = 0


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

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

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

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


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

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

def Beverage():
    global num1
    global num2
    global num3
    global num4
    Beverage = raw_input("what Beverage would you like today?")

    if (Beverage == "Soda"):
        num4 = 3
        print(add(num1,num2,num3,num4))
    elif (Beverage == "Beer"):
        num4 = 8
        print(add(num1,num2,num3,num4))
    elif (Beverage == "Wine"):
        num4 = 12
        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(a,b,c,d) :
    return a+b+c+d



Fruit()

您好,非常感谢您的回复。我将变量更改为F1,而不是F2,而不是main,依此类推。我也加入了全球术语,但最后我仍然有同样的错误!!!!!请帮忙谢谢,这解决了第二个问题Hi,非常感谢您的回复。我将变量改为F1,而不是F2,而不是main,依此类推。我也加入了全球术语,但最后我仍然有同样的错误!!!!!请帮忙谢谢,这解决了第二个问题