kurt.L kurt.L - 2 months ago 12
Python Question

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. 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.
Error1

Here is a picture of what I get if I type something that is not on the menu.
Error2

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

Answer

You used functions to define num1,num2... values , which becomes local variables . whereas you should have made these variables out of function . And use those variables in other functions using the keyword '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()
Comments