In: Computer Science
Not sure how to prompt these functions here. I am trying to create a holiday shopping list. Here are my three requirements
'''
This function should prompt the user for the names of all the family members that they will be purchasing gifts for this holiday and return those names in a list
'''
def family_names():
names = []
return names
'''
This function takes the names of all family members as a list and asks the user how much money they will spend on each person. These values are stored in a NEW list and returned.
'''
def spend(family):
pass
'''
This function takes two lists as parameters, one that has the names of all family members and a second that has how much was spent on each family member. The function calculates which family member is getting the most expensive gift and prints to the screen. Who this person is and how much is being spent on them. This function does not return anything.
'''
def favorite(family,gifts):
pass
#-----main-----
people = family_names()
money = spend(people)
favorite(people,money)
# program to create functions for holiday shopping list
# this function prompts for all the names
def family_names():
names = [] # empty list
while True:
entry = input ("Enter name:
")
names.append(entry)
while True:
choice =
input("Do you want to add more names? (Y/N): ").upper()
if choice == "Y"
or choice == "N":
break
# if N is entered, stop taking
new names
if choice == "N":
break
return names
# this function stores how much to spend in a new list
def spend(family):
# create empty list
budget = []
for member in family:
money = float(input ("How much do
you want to spend for {0}: ".format(member)))
budget.append(money)
return budget
# prints details
def favorite(family, gifts):
favorite = -1 # index position of the favorite family
member
maxspent = -1 # default maximum spent yet
for i in range (0, len(gifts)):
if gifts[i] > maxspent:
maxspent =
gifts[i]
favorite = i #
update index position of favorite member
# print the result
print ("Favorite family member is
{0}.".format(family[favorite]))
print ("Amount spent on them is
{0}\n".format(gifts[favorite]))
# main
if __name__ == "__main__":
people = family_names()
money = spend(people)
favorite(people, money)
Screenshot of the code:
OUTPUT: