0
Oop
I can’t understand what do this code Can anyone help me class Pizza: def __init__(self, toppings): self.toppings = toppings @staticmethod def validate_topping(topping): if topping == "pineapple": raise ValueError("No pineapples!") else: return True ingredients = ["cheese", "onions", "spam"] if all(Pizza.validate_topping(i) for i in ingredients): pizza = Pizza(ingredients)
3 Antworten
+ 9
Okay I'll explain it to you
#Create a Pizza class
class Pizza:
def __init__(self, toppings):
self.toppings = toppings
@staticmethod # What this does is create a function like def for example without using self, receiving an argument
def validate_topping(topping):
#What it does here is evaluate if the argument is equal to pineapple
if topping == "pineapple":
#If it turns out green, it will cause a created error to be thrown that says No pineapple
raise ValueError("No pineapples!")
else:
#Otherwise it will return True
return True
#Here is where to create a list of ingredients
ingredients = ["cheese", "onions", "spam"]
#What it will do is iterate the ingredients and pass each ingredient to the validate_topping method and the all function is used to verify if all the ingredients are valid, otherwise it will not print anything
if all(Pizza.validate_topping(i) for i in ingredients):
pizza = Pizza(ingredients)
+ 4
Begin down the bottom.
You have 3 ingredients.
There's an if statement which iterates through those ingredients using "i" and checks them against the method to see if an ingredient is pineapple and raise a value error.
If the check is successfully passed I.e. there isn't any pineapple, then the pizza may exist.
0
Thanks a lot
Can you explain more