added week 6 examples

This commit is contained in:
left_adjoint 2023-11-04 18:23:16 -07:00
parent 689c8e0673
commit b7af9a08b7
4 changed files with 155 additions and 16 deletions

View File

@ -1,19 +1,32 @@
# my own example of the hobbies discussion prompt
# we need to write a main function in order to
def main():
# boolean variable to control
toContinue = True
hobbies = []
while toContinue:
hobby = input("Enter one of your hobbies: ")
if hobby.lower() == "quit":
toContinue = False
print("Your hobbies were: ")
for h in hobbies:
print(h)
else:
hobbies.append(hobby)
# gonna put something in here
yourHobbies = []
hobby = input("What is one of your hobbies? ")
while hobby != "":
yourHobbies.append(hobby)
hobby = input("What is another of your hobbies? ")
outputStr = "Your favorite hobbies are "
#for h in yourHobbies:
# outputStr = outputStr + h + ", "
# alt solution 1: The Boring Way
#for i in range(0,len(yourHobbies)):
# if i < len(yourHobbies) -1:
# outputStr = outputStr + yourHobbies[i] + ", "
# else:
# outputStr = outputStr + "and " + yourHobbies[i] + "."
# alt solution 2
#for i in range(0,len(yourHobbies)-1):
# outputStr = outputStr + yourHobbies[i] + ", "
#outputStr = outputStr + "and " + yourHobbies[-1] + "."
# alt solution 3
for i,h in enumerate(yourHobbies):
if i != len(yourHobbies)-1:
outputStr = outputStr + h + ", "
else:
outputStr = outputStr + "and " + h + "."
print(outputStr)
main()

View File

@ -0,0 +1,37 @@
# 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()

39
week6/inks.py Normal file
View File

@ -0,0 +1,39 @@
def main():
inks = []
toContinue = True
while toContinue:
option = input("Enter an ink? Yes/No: ").lower()
if option == "yes":
inkN = input("Enter the name of the ink: ").lower()
inkC = input("Enter the color of the ink: ").lower()
shimmered = input("Is it shimmered? Yes/No: ").lower()
inks.append((inkN,inkC,shimmered))
elif option == "no":
toContinue = False
else:
print("Sorry, couldn't understand that")
#print(f"These are our ink names {inkNames}")
#print(f"These are the ink colors {inkColors}")
#print(f"This is whether they have shimmer {isShimmered}")
print("Ink Name | Ink Color | Shimmered?")
for n,c,s in inks:
print(f"{n} | {c} | {s}")
# count how many have shimmer
numShimmered = 0
largestNumColors = 0
largestColor = ""
for _,c,s in inks:
if s == "yes":
numShimmered = numShimmered + 1
numTimes = 0
for _,d,_ in inks:
if d == c:
numTimes = numTimes + 1
if numTimes > largestNumColors:
largestNumColors = numTimes
largestColor = c
print(f"You have {numShimmered} inks with glitter. Watch out for clogs.")
print(f"The color you have the most of is {largestColor} and you have {largestNumColors} inks of this color.")
main()

50
week6/inksTuple.py Normal file
View File

@ -0,0 +1,50 @@
def main():
inkNames = []
inkColors = []
isShimmered = []
toContinue = True
while toContinue:
option = input("Enter an ink? Yes/No: ").lower()
if option == "yes":
inkN = input("Enter the name of the ink: ").lower()
inkC = input("Enter the color of the ink: ").lower()
shimmered = input("Is it shimmered? Yes/No: ").lower()
inkNames.append(inkN)
inkColors.append(inkC)
isShimmered.append(shimmered)
elif option == "no":
toContinue = False
else:
print("Sorry, couldn't understand that")
#print(f"These are our ink names {inkNames}")
#print(f"These are the ink colors {inkColors}")
#print(f"This is whether they have shimmer {isShimmered}")
print("Ink Name | Ink Color | Shimmered?")
for i in range(0, len(inkNames)):
print(f"{inkNames[i]} | {inkColors[i]} | {isShimmered[i]}")
# count how many have shimmer
numShimmered = 0
for s in isShimmered:
if s == "yes":
numShimmered = numShimmered + 1
print(f"You have {numShimmered} inks with glitter. Watch out for clogs.")
# count how many inks of the same color there are
largestNumColors = 0
largestColor = ""
# for each color c in the list of colors
# we need to count how many times it shows up in the list
# we do this in the inner loop (the one with d as the loop variable)
# otherwise, it's just like finding the max element of a list of numbers
for c in inkColors:
numTimes = 0
for d in inkColors:
if d == c:
numTimes = numTimes + 1
if numTimes > largestNumColors:
largestNumColors = numTimes
largestColor = c
print(f"The color you have the most of is {largestColor} and you have {largestNumColors} inks of this color.")
main()