Use your Food class, utilities code, and sample data from Lab 1 to complete the following Tasks.
def average_calories(foods):
"""
-------------------------------------------------------
Determines the average calories in a list of
foods.
foods is unchanged.
Use: avg = average_calories(foods)
-------------------------------------------------------
Parameters:
foods - a list of Food
objects (list of Food)
Returns:
avg - average calories
in all Food objects of foods (int)
-------------------------------------------------------
"""
your code
here is the data
Ravioli, 7, False, 246
here is the Food class page
class Food:
"""
Defines an object for a single food: name,
origin, vegetarian, calories.
"""
# Constants
ORIGIN = ("Canadian", "Chinese", "Indian",
"Ethiopian",
"Mexican", "Greek", "Japanese", "Italian", "American",
"Scottish", "New Zealand", "English")
@staticmethod
def origins():
"""
-------------------------------------------------------
Creates a string list of
food origins in the format:
0
Canadian
1
Chinese
2
Indian
...
Use: s =
Food.origins()
Use:
print(Food.origins())
-------------------------------------------------------
Returns:
string - A numbered list of valid food origins.
-------------------------------------------------------
"""
string = ""
for i in
range(len(Food.ORIGIN)):
string += """{:2d} {}
""".format(i, Food.ORIGIN[i])
return string
# your code here
return string
def __init__(self, name, origin,
is_vegetarian, calories):
"""
-------------------------------------------------------
Initialize a food
object.
Use: f = Food( name,
origin, is_vegetarian, calories )
-------------------------------------------------------
Parameters:
name - food name (str)
origin - food origin (int)
is_vegetarian - whether food is vegetarian (boolean)
calories - caloric content of food (int >= 0)
Returns:
A new Food object (Food)
-------------------------------------------------------
"""
assert origin in
range(len(Food.ORIGIN)), "Invalid origin ID"
assert is_vegetarian in
(True, False, None), "Must be True or False"
assert calories is None
or calories >= 0, "Calories must be >= 0"
self.name =
name
self.origin =
origin
self.is_vegetarian =
is_vegetarian
self.calories =
calories
return
def __str__(self):
"""
-------------------------------------------------------
Creates a formatted
string of food data.
Use: print(f)
Use: s = str(f)
-------------------------------------------------------
Returns:
string - the formatted contents of food (str)
-------------------------------------------------------
"""
string =
"""Name: {}
Origin: {}
Vegetarian: {}
Calories: {}""".format(self.name,
Food.ORIGIN[self.origin], self.is_vegetarian, self.calories)
# your code here
return string
def __eq__(self, rs):
"""
-------------------------------------------------------
Compares this food
against another food for equality.
Use: f == rs
-------------------------------------------------------
Parameters:
rs - [right side] food to compare to (Food)
Returns:
result - True if name and origin match, False otherwise
(boolean)
-------------------------------------------------------
"""
result =
(self.name.lower(), self.origin) == (
rs.name.lower(), rs.origin)
return result
def __lt__(self, rs):
"""
-------------------------------------------------------
Determines if this food
comes before another.
Use: f < rs
-------------------------------------------------------
Parameters:
rs - [right side] food to compare to (Food)
Returns:
result - True if food precedes rs, False otherwise (boolean)
-------------------------------------------------------
"""
result =
(self.name.lower(), self.origin) < \
(rs.name.lower(), rs.origin)
return result
def __le__(self, rs):
"""
-------------------------------------------------------
Determines if this food
precedes or is or equal to another.
Use: f <= rs
-------------------------------------------------------
Parameters:
rs - [right side] food to compare to (Food)
Returns:
result - True if this food precedes or is equal to rs,
False otherwise (boolean)
-------------------------------------------------------
"""
result = self < rs or
self == rs
return result
def write(self, file_variable):
"""
-------------------------------------------------------
Writes a single line of
food data to an open file.
Use:
f.write(file_variable)
-------------------------------------------------------
Parameters:
file_variable - an open file of food data (file)
Returns:
The contents of food are written as a string in the format
name|origin|is_vegetarian to file_variable.
-------------------------------------------------------
"""
print("{}|{}|{}|{}"
.format(self.name, self.origin, self.is_vegetarian,
self.calories),
file=file_variable)
return
def key(self):
"""
-------------------------------------------------------
Creates a formatted
string of food key data.
Use: key = f.key()
-------------------------------------------------------
Returns:
the formatted contents of food key (str)
-------------------------------------------------------
"""
return "{},
{}".format(self.name, self.origin)
def __hash__(self):
"""
-------------------------------------------------------
Generates a hash value
from a food name.
Use: h = hash(f)
-------------------------------------------------------
Returns:
value - the total of the characters in the name string (int >
0)
-------------------------------------------------------
"""
value = 0
for c in
self.name:
value = value + ord(c)
return value
def average_calories(foods):
"""
-------------------------------------------------------
Determines the average calories in a list of foods.
foods is unchanged.
Use: avg = average_calories(foods)
-------------------------------------------------------
Parameters:
foods - a list of Food objects (list of Food)
Returns:
avg - average calories in all Food objects of foods (int)
-------------------------------------------------------
"""
totalCalories=0
for food in foods:
totalCalories+=food.calories
return totalCalories//len(foods)
Use your Food class, utilities code, and sample data from Lab 1 to complete the following...
Python3 programming help needed LAB*: Program: Online shopping cart (continued) Need the below code edited to run properly for the lab.. class ItemPurchase: def __init__(self, nameitem='none', item_prc=0, item_quntity=0, item_descrp = 'none'): self.nameitem = nameitem self.item_prc = item_prc self.item_quntity = item_quntity self.item_descrp = item_descrp def print_itemvaluecost(self): string = '{} {} @ ${} = ${}'.format(self.nameitem, self.item_quntity, self.item_prc(self.item_quntity * self.item_prc)) valuecost = self.item_quntity * self.item_prc return string, valuecost def print_itemdescription(self): string...
Python3 programming help needed LAB*: Program: Online shopping cart (continued) Need the below code edited to run properly for the lab.. class ItemPurchase: def __init__(self, nameitem='none', item_prc=0, item_quntity=0, item_descrp = 'none'): self.nameitem = nameitem self.item_prc = item_prc self.item_quntity = item_quntity self.item_descrp = item_descrp def print_itemvaluecost(self): string = '{} {} @ ${} = ${}'.format(self.nameitem, self.item_quntity, self.item_prc(self.item_quntity * self.item_prc)) valuecost = self.item_quntity * self.item_prc return string, valuecost def print_itemdescription(self): string = '{}: {}'.format(self.nameitem, self.item_descrp) print(string , end='\n') return string class Shopping_Cart: #Parameter...
I need help with my code. It keeps getting this error: The assignment is: Create a class to represent a Food object. Use the description provided below in UML. Food name : String calories : int Food(String, int) // The only constructor. Food name and calories must be // specified setName(String) : void // Sets the name of the Food getName() : String // Returns the name of the Food setCalories(int) : void // Sets the calories of the Food...
PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList: def __init__(self): self.__head = None self.__tail = None self.__size = 0 def insert(self, i, data): if self.isEmpty(): self.__head = listNode(data) self.__tail = self.__head elif i <= 0: self.__head = listNode(data, self.__head) elif i >= self.__size: self.__tail.setNext(listNode(data)) self.__tail = self.__tail.getNext() else: current = self.__getIthNode(i - 1) current.setNext(listNode(data,...
PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...
Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user if they want to add a customer to the queue, serve the next customer in the queue, or exit. When a customer is served or added to the queue, the program should print out the name of that customer and the remaining customers in the queue. The store has two queues: one is for normal customers, another is for VIP customers. Normal customers can...
1. Code a breadth First traversal. 2. Code a depth First traversal. Note, your traversals should return a string listing the nodes added to the min spanning tree and the order they are added. Using Python. Starter code: from collections import deque class Edge: def __init__(self, endIndex, next = None): self.endIndex = endIndex self.next = next class Node: def __init__(self, name): self.name = name self.visited = False self.connects = None class Graph: def __init__(self): self.nodeList = [] self.size = 20...
Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this is in Zybooks Existing Code # Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price,...
class Livestock: def __init__(self,name,price_in,utilizations): self.name,self.price_in,self.utilizations = name,float(price_in),utilizations def __lt__(self,other): if self.utilizations is None: return True elif other.utilizations is None: return False else: return self.utilizations.count(';') < other.utilizations.count(';') def __eq__(self,other): return self.name == other.name def __repr__(self): return ("{}, {}".format(self.name,self.price_in)) raw_livestock_data = [ # name, price_in, utilizations ['Dog', '200.0', 'Draught,Hunting,Herding,Searching,Guarding.'], ['Goat', '1000.0', 'Dairy,Meat,Wool,Leather.'], ['Python', '10000.3', ''], ['Cattle', '2000.75', 'Meat,Dairy,Leather,Draught.'], ['Donkey', '3400.01', 'Draught,Meat,Dairy.'], ['Pig', '900.5', 'Meat,Leather.'], ['Llama', '5000.66', 'Draught,Meat,Wool.'], ['Deer', '920.32', 'Meat,Leather.'], ['Sheep', '1300.12', 'Wool,Dairy,Leather,Meat.'], ['Rabbit', '100.0', 'Meat,Fur.'], ['Camel', '1800.9', 'Meat,Dairy,Mount.'], ['Reindeer', '4000.55', 'Meat,Leather,Dairy,Draught.'],...