I am trying print an element that is in a list that has been sorted and stored into a variable. I want to be able to print the element that is stored in the list, in this case ice cream flavors, based on user input. This is for an assignment for a class. I feel I have most of the code working and I am able to call elements from the dictionary I created but not my list. Any suggestions or tips. Maybe I'm not asking the question correctly which oftentimes happens.
#Create flavor list
flavorList = ['Vanilla', 'Chocolate', 'Strawberry','Pistachio','Butter Pecan', 'Cookie Dough', 'Neapolitan']
flavorList[6] = 'Tiramisu' #Change value of one flavor
flavorInput = input("Add additional flavor of your choice: ")
flavorList.append(flavorInput) #Sort list in alphabetical order
sorted_flavors = sorted(flavorList)
print('What flavor of ice cream would you like?')
#Indexes the flavor starting from 1
for flavIndex, sorted_flavors in enumerate (sorted_flavors, start=1):
print(flavIndex, sorted_flavors)
flavorSelection = input('Select numbers 1-8 for your desired flavor: ')
flavorSelection = int(flavorSelection) #stores user flavor selection in a variable
theFlavor = flavorSelection
theFlavor = theFlavor -1
theFlavor = int(theFlavor)
#Create dictionaries for flavor prices and sizes
conePrices = dict(S='$1.50', M='$2.50', L='$3.50')
coneSizes = dict(S='Tiny', M='Regular Joe', L='Big Fella')
sizeSelection = input('What size would you like?: \n' + "Press S for small, M for Medium and L for Large:")
sizeSelection = sizeSelection.upper()
sizeSelection = str(sizeSelection)
theSize = coneSizes[sizeSelection]
thePrice = conePrices[sizeSelection]
print('Okay your '+ theSize, sorted_flavors[theFlavor] + 'ice cream costs ' + thePrice + '.')
print( 'Please wait just a moment and your order will arrive shortly... ')
You can access an item from a list with list[index]
:
#Create flavor list
flavorList = ['Vanilla', 'Chocolate', 'Strawberry','Pistachio','Butter Pecan', 'Cookie Dough', 'Neapolitan']
flavorList[6] = 'Tiramisu' #Change value of one flavor
flavorInput = input("Add additional flavor of your choice: ")
flavorList.append(flavorInput) #Sort list in alphabetical order
sorted_flavors = sorted(flavorList)
print('What flavor of ice cream would you like?')
#Indexes the flavor starting from 1
for flavIndex, sorted_flavor in enumerate (sorted_flavors, start=1):
print(flavIndex, sorted_flavor)
flavorSelection = input('Select numbers 1-8 for your desired flavor: ')
flavorSelection = int(flavorSelection) #stores user flavor selection in a variable
flavorSelection -= 1 # Because lists start at index 0.
print(f"Your flovor : {sorted_flavors[flavorSelection]}") # Print the flavor.
Output:
Add additional flavor of your choice: Blueberry
What flavor of ice cream would you like?
1 Blueberry
2 Butter Pecan
3 Chocolate
4 Cookie Dough
5 Pistachio
6 Strawberry
7 Tiramisu
8 Vanilla
Select numbers 1-8 for your desired flavor: 5
Your flovor : Pistachio
In this line
#Indexes the flavor starting from 1
for flavIndex, *sorted_flavors* in enumerate (sorted_flavors, start=1):
print(flavIndex, sorted_flavors)
You are overwriting the value of sorted_flavors
with the value of a string contained within sorted_flavors
. It's actually sort of weird that you code still even works, Anyways, if you replace this with
#Indexes the flavor starting from 1
for flavIndex, flavor in enumerate (sorted_flavors, start=1):
print(flavIndex, flavor)
It works. The entire fixed code is below.
#Create flavor list
flavorList = ['Vanilla', 'Chocolate', 'Strawberry','Pistachio','Butter Pecan', 'Cookie Dough', 'Neapolitan']
flavorList[6] = 'Tiramisu' #Change value of one flavor
flavorInput = input("Add additional flavor of your choice: ")
flavorList.append(flavorInput) #Sort list in alphabetical order
sorted_flavors = sorted(flavorList)
print('What flavor of ice cream would you like?')
#Indexes the flavor starting from 1
for flavIndex, flavor in enumerate (sorted_flavors, start=1):
print(flavIndex, flavor)
flavorSelection = int(input('Select numbers 1-8 for your desired flavor: ')) - 1
#Create dictionaries for flavor prices and sizes
conePrices = dict(S='$1.50', M='$2.50', L='$3.50')
coneSizes = dict(S='Tiny', M='Regular Joe', L='Big Fella')
sizeSelection = input('What size would you like?: \n' + "Press S for small, M for Medium and L for Large:")
sizeSelection = sizeSelection.upper()
theSize = coneSizes[sizeSelection]
thePrice = conePrices[sizeSelection]
print('Okay your '+ theSize + ' ' + sorted_flavors[flavorSelection] + ' ice cream costs ' + thePrice + '.')
print( 'Please wait just a moment and your order will arrive shortly... ')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.