cs160examples/week6/hobbiesDictionary.py

38 lines
1.0 KiB
Python

# now need to modify this to use a dictionary to store
# users and their hobbies
# then print all of it out at the end
def askForHobbies():
hobbies = []
hobby = input("What is one of your hobbies? ")
while hobby != "":
hobbies.append(hobby)
hobby = input("What is another of your hobbies? ")
return hobbies
def printHobbies(name, hobbies):
outputStr = name + "'s" + " favorite hobbies are "
if len(hobbies) > 1:
for i, h in enumerate(hobbies):
if i != len(hobbies) - 1:
outputStr = outputStr + h + ", "
else:
outputStr = outputStr + "and " + h + "."
else:
outputStr = outputStr + hobbies[0] + "."
print(outputStr)
def main():
# gonna put something in here
people = {}
name = input("Who's hobbies are these? Quit to exit: ")
while name.lower() != "quit":
theirHobbies = askForHobbies()
people[name] = theirHobbies
name = input("Who else's hobbies do you want to enter? Quit to exit: ")
print("Here's everyone's hobbies: ")
for n,hs in people.items():
printHobbies(n,hs)
main()