Home > Mobile >  Having issues with python functions
Having issues with python functions

Time:10-25

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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