0

I am making a couple of functions for taking pizza information in one, then using that information to calculate the price in the other function. When I run this though, it's having problems locating attributes in one of the functions, even after its run.

def calc_pizza_charge(size_cost, meats_cost, veg_cost, num_cost):
    get_pizza_info.size = range(1, 4)
    num_cost = get_pizza_info.quantity
    total = (size_cost + meats_cost + veg_cost) * get_pizza_info.quantity
    if get_pizza_info.size == 1:
        size_cost = 6.50
    if get_pizza_info.size == 2:
        size_cost = 9.50
    if get_pizza_info.size == 3:
        size_cost = 11.50
    meats_cost = (get_pizza_info.meats - 1) * 3.50
    veg_cost = (get_pizza_info.veg - 1) * 1.50

    print("Your total is $", "{:,.2f}".format(total))


def get_pizza_info(size, meats, veg, quantity):
    size = int(input("Enter size from 1-3: "))
    meats = int(input("Enter number of meat toppings "))
    veg = int(input("Enter number of non-meat toppings "))
    quantity = int(input("Enter number of these pizzas "))
    if size >= 4:
        size = 3
    if size <= 1:
        size = 1
    if meats <= 1:
        meats = 1
    if veg <= 1:
        veg = 1
3
  • What is the error that you see? Commented Oct 24, 2021 at 14:22
  • 2
    I don't think that's how functions work. Commented Oct 24, 2021 at 14:23
  • I did notice the redundancy between quantity and num_cost. I'm not calling them here, I'm calling them in a separate project page/whatever it's called. Thanks EmeraldThunder for the wonderful guidance. Commented Oct 24, 2021 at 14:42

3 Answers 3

1

Since you are not comfortable with other answers, Here a working code with minimal modifications to your code

def calc_pizza_charge():
    size, meats, veg, quantity = get_pizza_info()

    if size == 1:
        size_cost = 6.50
    elif size == 2:
        size_cost = 9.50
    else:
        size_cost = 11.50

    meats_cost = (meats - 1) * 3.50
    veg_cost = (veg - 1) * 1.50

    total = (size_cost + meats_cost + veg_cost) * quantity

    print("Your total is $", "{:,.2f}".format(total))


def get_pizza_info():
    size = int(input("Enter size from 1-3: "))
    meats = int(input("Enter number of meat toppings "))
    veg = int(input("Enter number of non-meat toppings "))
    quantity = int(input("Enter number of these pizzas "))
    if size >= 4:
        size = 3
    elif size <= 1:
        size = 1
    if meats <= 1:
        meats = 1
    if veg <= 1:
        veg = 1

    return size, meats, veg, quantity


if __name__ == '__main__':
    calc_pizza_charge()
Sign up to request clarification or add additional context in comments.

Comments

1

To be honest, I was extremely confused when I saw your code. I think you should review how functions work. Anyway, a few pointers:

  • In get_pizza_info you essentially ask for the order details: store it in some sort of data structure (I used dictionary)
  • In get_pizza_charge, you need to use the order details to compute the price of the pizza.

I have included my rewriting of your code:

def calc_pizza_charge():
    pizza_info = get_pizza_info()
    num_cost = pizza_info["quantity"]
    size_cost = get_size_cost(pizza_info["size"])
    meats_cost = (pizza_info["meats"] - 1) * 3.50
    veg_cost = (pizza_info["veg"] - 1) * 1.50
    total = (size_cost + meats_cost + veg_cost) * pizza_info["quantity"]

    print("Your total is $", "{:,.2f}".format(total))


def get_size_cost(size):
    if size == 1:
        return 6.5
    elif size == 2:
        return 9.5
    elif size == 3:
        return 11.50

def get_pizza_info():
    size = int(input("Enter size from 1-3: "))
    meats = int(input("Enter number of meat toppings "))
    veg = int(input("Enter number of non-meat toppings "))
    quantity = int(input("Enter number of these pizzas "))
    pizza_info = {}
    pizza_info["size"] = max(min(size, 3), 1)
    pizza_info["meats"] = max(1, meats)
    pizza_info["veg"] = max(1, veg)
    pizza_info["quantity"] = max(0, quantity)
    return pizza_info


calc_pizza_charge()

1 Comment

Thank you nikeros. We haven't gotten into dictionaries and lists yet. This is just my first semester of my first programming class. Looking at my code, what specifically do you see that is wrong?
1

You have defined all the attribute variables in get_pizza_info function and are trying to access them in calc_pizza_charge function. Variables declared in a function are local to that function and cannot be accessed by any entity outside that function.

For your problem I would suggest defining a new class that contains both of these functions.

class Pizza:
    def calc_pizza_charge(self):
        num_cost = self.quantity
        if self.size == 1:
            size_cost = 6.50
        if self.size == 2:
            size_cost = 9.50
        if self.size == 3:
            size_cost = 11.50
        else:
            size_cost = 10
        meats_cost = (self.meats - 1) * 3.50
        veg_cost = (self.veg - 1) * 1.50
        total = (size_cost + meats_cost + veg_cost) * self.quantity
        print("Your total is $", "{:,.2f}".format(total))


    def get_pizza_info(self):
        self.size = int(input("Enter size from 1-3: "))
        self.meats = int(input("Enter number of meat toppings "))
        self.veg = int(input("Enter number of non-meat toppings "))
        self.quantity = int(input("Enter number of these pizzas "))
        if self.size >= 4:
            self.size = 3
        if self.size <= 1:
            self.size = 1
        if self.meats <= 1:
            self.meats = 1
        if self.veg <= 1:
            self.veg = 1
        
piz = Pizza()
piz.get_pizza_info()
piz.calc_pizza_charge()

This code runs fine just like you wanted it to work. There were many fundamental mistakes in your code. I would suggest you to learn basics of functions and classes in Python.

1 Comment

Yogiraj, we haven't learned classes yet. So, without using classes, how can I get these two functions to work together?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.