LAB2-2
.py
keyboard_arrow_up
School
University of Pennsylvania *
*We aren’t endorsed by this school
Course
MISC
Subject
Mathematics
Date
Jan 9, 2024
Type
py
Pages
8
Uploaded by ColonelPencilSandpiper19
# LAB2
# REMINDER: The work in this assignment must be your own original work and must be completed alone.
import math
# -------- SECTION 1
class Instructor:
'''
>>> t1= Instructor('John Doe')
>>> t1.get_name()
'John Doe'
>>> t1.get_courses()
[]
>>> t1.add_course('MATH140')
>>> t1.get_courses()
['MATH140']
>>> t1.add_course('STAT100')
>>> t1.get_courses()
['MATH140', 'STAT100']
>>> t1.add_course('STAT100')
>>> t1.get_courses()
['MATH140', 'STAT100']
>>> t1.remove_course('MATH141')
>>> t1.get_courses()
['MATH140', 'STAT100']
>>> t1.remove_course('MATH140')
>>> t1.get_courses()
['STAT100']
'''
def __init__(self, name):
self.name=name
self.courses=[]
def get_name(self):
return self.name
def set_name(self, new_name):
if new_name!="":
self.name=new_name
def get_courses(self):
return self.courses
def remove_course(self, course):
if course in self.courses:
self.courses.remove(course)
def add_course(self,course):
if not course in self.courses:
self.courses+=[course]
# -------- SECTION 2 class Pantry:
""""
>>> sara_pantry = Pantry()
>>> sara_pantry.stock_pantry('Bread', 2)
'Pantry Stock for Bread: 2.0'
>>> sara_pantry.stock_pantry('Cookies', 6) 'Pantry Stock for Cookies: 6.0'
>>> sara_pantry.stock_pantry('Chocolate', 4) 'Pantry Stock for Chocolate: 4.0'
>>> sara_pantry.stock_pantry('Pasta', 3) 'Pantry Stock for Pasta: 3.0'
>>> sara_pantry
I am a Pantry object, my current stock is {'Bread': 2.0, 'Cookies': 6.0, 'Chocolate': 4.0, 'Pasta': 3.0}
>>> sara_pantry.get_item('Pasta', 2) 'You have 1.0 of Pasta left'
>>> sara_pantry.get_item('Pasta', 6) 'Add Pasta to your shopping list!'
>>> sara_pantry
I am a Pantry object, my current stock is {'Bread': 2.0, 'Cookies': 6.0, 'Chocolate': 4.0, 'Pasta': 0.0}
>>> ben_pantry = Pantry() >>> ben_pantry.stock_pantry('Cereal', 2)
'Pantry Stock for Cereal: 2.0'
>>> ben_pantry.stock_pantry('Noodles', 5) 'Pantry Stock for Noodles: 5.0'
>>> ben_pantry.stock_pantry('Cookies', 9) 'Pantry Stock for Cookies: 9.0'
>>> ben_pantry.stock_pantry('Cookies', 8) 'Pantry Stock for Cookies: 17.0'
>>> ben_pantry.get_item('Pasta', 2) "You don't have Pasta"
>>> ben_pantry.get_item('Cookies', 2.5) 'You have 14.5 of Cookies left'
>>> sara_pantry.transfer(ben_pantry, 'Cookies')
>>> sara_pantry
I am a Pantry object, my current stock is {'Bread': 2.0, 'Cookies': 20.5, 'Chocolate': 4.0, 'Pasta': 0.0}
>>> ben_pantry.transfer(sara_pantry, 'Rice')
>>> ben_pantry.transfer(sara_pantry, 'Pasta')
>>> ben_pantry
I am a Pantry object, my current stock is {'Cereal': 2.0, 'Noodles': 5.0, 'Cookies': 0.0}
>>> ben_pantry.transfer(sara_pantry, 'Pasta')
>>> ben_pantry
I am a Pantry object, my current stock is {'Cereal': 2.0, 'Noodles': 5.0, 'Cookies': 0.0}
>>> sara_pantry
I am a Pantry object, my current stock is {'Bread': 2.0, 'Cookies': 20.5, 'Chocolate': 4.0, 'Pasta': 0.0}
"""
def __init__(self):
self.items = {}
def __repr__(self):
return "I am a Pantry object, my current stock is {}".format(self.items)
def stock_pantry(self, item, qty):
if not item in self.items:
self.items[item]=float(qty)
else:
self.items[item]+=float(qty)
return "Pantry Stock for {}: {}".format(item,self.items[item])
def get_item(self, item, qty):
if not item in self.items:
return "You don't have {}".format(item)
else:
if self.items[item]>qty:
self.items[item]-=qty
return f"You have {self.items[item]} of {item} left"
else:
self.items[item]=0.0
return f"Add {item} to your shopping list!"
def transfer(self, other_pantry, item):
if item in other_pantry.items:
if other_pantry.items[item]>0.0:
if not item in self.items:
self.items[item]=other_pantry.items[item]
other_pantry.items[item]=0.0
else:
self.items[item]+=other_pantry.items[item]
other_pantry.items[item]=0.0
# -------- SECTION 3
class Player:
"""
>>> p1 = Player('Susy')
>>> print(p1)
No game records for Susy
>>> p1.update_loss()
>>> p1
*Game records for Susy*
Total games: 1
Games won: 0
Games lost: 1
Best game: None
>>> p1.update_win(5)
>>> p1.update_win(2)
>>> p1
*Game records for Susy*
Total games: 3
Games won: 2
Games lost: 1
Best game: 2 attempts
"""
def __init__(self, name):
self.name=name
self.total_games=0
self.won=0
self.lost=0
self.best=None
def update_win(self, att):
self.won+=1
self.total_games+=1
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help