简体   繁体   English

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

[英]Python Error TypeError: unsupported operand type(s) for +: 'function' and 'function'

I know there is some stuff online about this error but whatever I try just doesn't seem to fix it. 我知道网上有一些关于此错误的信息,但是我尝试执行的任何操作似乎都无法解决。 I just started learning Python yesterday and am an absolute beginner so please don't judge my script. 我昨天才刚开始学习Python,并且是一个绝对的初学者,所以请不要判断我的脚本。 It is just a simple script wanting to take the order of a customer from a restaurant and just calculate the total price of the meal. 这只是一个简单的脚本,想要从餐厅接下顾客的订单,然后只计算餐点的总价。 It's pretty cheesy but I would love some help. 这很俗气,但我想帮忙。 I can not get my script to calculate the total cost of the meal and whatever I try, it just doesn't work. 我无法获得脚本来计算餐点的总费用,无论我如何尝试,都无法正常工作。 Could you please tell me what I am doing wrong and how to calculate the total cost of the meal based on what the customer chooses. 您能告诉我我做错了什么,以及如何根据客户的选择来计算餐点的总费用。 Also If they pick an item that is not on the menu, the program closes instead of letting them try again. 另外,如果他们选择菜单上没有的项目,程序将关闭而不是让他们再试一次。 Why? 为什么? I would greatly appreciate your help. 非常感谢您的帮助。 :) THANKS :) 谢谢

Here is a picture of the cost error I get when running the script in terminal. 这是在终端中运行脚本时遇到的成本错误的图片。 错误1

Here is a picture of what I get if I type something that is not on the menu. 这是我输入菜单上未输入的内容的图片。 错误2

Here is my script. 这是我的剧本。

Note, I have aded the items from the menu just to show you what they are and their cost. 请注意,我从菜单中添加了一些项目,只是为了向您展示它们的种类和成本。

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

You are redifining your function names with your local variable names 您正在使用本地变量名称重新定义函数名称

No wonder why there is a mixup. 难怪为什么会有混淆。

I know visual basic uses to do that for return values but you cannot do that in python. 我知道视觉基本用法可用于返回值,但您无法在python中做到这一点。

Just rename your local variables (the ones assigned to result of raw_input and it will be ok 只需重命名您的局部变量(分配给raw_input结果的变量,就可以了

You shouldn't name functions the same as variables, ie the fruit function should look like this: 您不应将函数的名称与变量相同,即水果函数应如下所示:

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

Also note the global , it makes fruit accesable outside the function Fruit() . 还要注意global ,它使fruit在函数Fruit()之外成为可访问的。 If you'd apply these changes to all functions, it should work 如果将这些更改应用于所有功能,则应该可以使用

If you also want to let people try again, you could simply call the function again, like in the function Fruit() . 如果您还想让人们再试一次,则可以再次调用该函数,就像在Fruit()函数中一样。

Hope this helps! 希望这可以帮助!

You used functions to define num1,num2... values , which becomes local variables . 您使用函数定义了num1,num2 ...值,这些值成为局部变量。 whereas you should have made these variables out of function . 而您应该使这些变量失效。 And use those variables in other functions using the keyword 'global'. 并在其他函数中使用关键字“ global”使用这些变量。

here's the edited code : 这是编辑后的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 错误:TypeError:不支持的操作数类型 -:'int' 和 'function' - Python Error: TypeError: unsupported operand type(s) for -: 'int' and 'function' 类型错误:不支持 - 的操作数类型:“函数”和“函数” - TypeError: unsupported operand type(s) for -: 'function' and 'function' Python TypeError:不支持的操作数类型 - :'int'和'function' - Python TypeError: unsupported operand type(s) for -: 'int' and 'function' TypeError:*:'function'和'int'不支持的操作数类型 - TypeError: unsupported operand type(s) for *: 'function' and 'int' 类型错误:* 不支持的操作数类型:'function' 和 'float' - TypeError: unsupported operand type(s) for *: 'function' and 'float' 类型错误:不支持 + 的操作数类型:'function' 和 'int' - TypeError: unsupported operand type(s) for +: 'function' and 'int' 类型错误:+= 不支持的操作数类型:'float' 和 'function' - TypeError: unsupported operand type(s) for +=: 'float' and 'function' 如何修复此错误:TypeError: unsupported operand type(s) for *: 'float' and 'function' in python - How do I fix this error: TypeError: unsupported operand type(s) for *: 'float' and 'function' in python python函数处理给出错误TypeError:不支持的操作数类型-:'NoneType'和'NoneType' - python function handling giving error TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType' python 中的错误“TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'int'” 对于非关键字变量名? - Error in python “ TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'int' ” for non-keyword varname?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM