Language is Python 3.6 HOMEWORK 02FRUITFUL FUNCTIONS & CONDITIONALS Part 1: Mark
ID: 3882414 • Letter: L
Question
Language is Python 3.6
HOMEWORK 02FRUITFUL FUNCTIONS & CONDITIONALS Part 1: Market Place You've just moved into your dorm room and now you need to do some shopping. Using the table below, write the following functions to help you at the grocery storeNOTEthe list price value is the usual price of the item, the current price value is how much the store is selling the item for right now. Item name B in stock List price Current price "avocadol" out of stock $1.5 $1 toothpaste" $2.75 $2.75 popcorn 1e S1 bottled water $5.5e "phone charges $15 $12 Function name (1): is_in_stock Parameters item (str), quantity (int) Return value, True or false (bool) or None (NoneType) Write a function that determines whether or not the parameter item is in stock. If so, you also need to check that there are enough of that item in stock to fulfill your order tapecified by the parameter Ouantity. Return True I those conditions are met, False otherwise. Return None if the item is not in the table. is_in_stock ("avocado", 3) False is in stock ("bottled water", 1) True is in stock ("popcorn", 1ee) False is_in_stock("potato chips", 5) None Function name I can afford Parameters item (str, quantity (int), wallet (int) Retum value True or False (bool) or None NoneType) Write a function that determines whether or not you can afford the parameter item, given the number of that team you would like to buy specified by parameter quantity, the amount of money you have specified by parameter wallet, and the item's current price value in the table. You do not need to consider whether there are enough of the item in stock for this function. Return True if you can afford this item, False otherwise. Return None if the item is not in the table. Test cases not an exhaustive list: can afford("avocado",500, 5) False can afford("phone charger10, 500) True , can affordsouthwash1, 20None ("", )Explanation / Answer
#!usr/bin/python
def is_in_stock(item, quantity):
index = -1
for i in range(len(item_name)):
if item == item_name[i]:
index = i
if index != -1:
if instock[index] >= quantity:
return True
else:
return False
else:
return None
def can_afford(item, quantity, wallet):
index = -1
for i in range(len(item_name)):
if item == item_name[i]:
index = i
if index != -1:
if wallet >= quantity * price[index]:
return True
else:
return False
else:
return None
def can_afford(item, quantity, wallet):
index = -1
for i in range(len(item_name)):
if item == item_name[i]:
index = i
if index != -1:
if wallet >= quantity * price[index]:
return True
else:
return False
else:
return None
def is_on_sale(item):
index = -1
for i in range(len(item_name)):
if item == item_name[i]:
index = i
if index != -1:
if price[index] < list_price[index]:
return True
else:
return False
else:
return None
def is_cheaper(item1, item2):
index1 = -1
index2 = -1
for i in range(len(item_name)):
if item1 == item_name[i]:
index1 = i
if item2 == item_name[i]:
index2 = i
if index1 != -1 and index2 != -1:
if price[index1] < price[index2]:
return 1
if price[index1] > price[index2]:
return -1
if price[index1] == price[index2]:
return 0
else:
return None
item_name = [ "avocado", "toothpaste", "popcorn", "bottled water", "phone charger"]
instock = [0,5,10,8,1]
list_price = [1.50,2.75,1,5.5,15]
price = [1,2.75,1,4,12]
print(is_in_stock("avocado",3))
print(is_in_stock("bottled water",1))
print(is_in_stock("popcorn",100))
print(is_in_stock("potato chips",5))
print(can_afford("avocado",500,5))
print(can_afford("phone charger",10,500))
print(can_afford("mouthwash",1,20))
print(is_on_sale("phone charger"))
print(is_on_sale("toothpaste"))
print(is_on_sale("chocolate bar"))
print(is_cheaper("popcorn","bottled water"))
print(is_cheaper("avocado","popcorn"))
print(is_cheaper("toothpaste","floss"))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.