CP164 Lecture 2: food_utilities class

1649 views5 pages
1 Aug 2018
School
Course
Professor

Document Summary

__updated__ = "2018-01-21" from food import food def get_food(): Creates a food object by requesting data from a user. Postconditions: returns food - a completed food object (food). name = input("name: ") print("origin") print(food. origins()) origin = int(input(": ")) s = input("vegetarian (y/n): ") # accept a range of values for vegetarian is_vegetarian = s. upper() in ("y", "true", "1", "t", "yes") calories = int(input("calories: ")) food = food(name, origin, is_vegetarian, calories) return food def read_food(line): Creates and returns a food object from a line of string data. Preconditions: line - a vertical bar-delimited line of food data in the format name|origin|is_vegetarian|calories (str) Postconditions: returns food - contains the data from line (food) data = line. strip(). split("|") food = food(data[0], int(data[1]), data[2] == "true", int(data[3])) return food def read_foods(file_variable): Reads a file of food strings into a list of food objects. Preconditions: file_variable - a file of food data (file)

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents