In: Computer Science
data = []
infile = open("Mac_menu.csv", "r")
line = infile.readline()
for line in infile:
line = line.rstrip("\n")
result = tuple(line.split(","))
data.append(result)
infile.close()
def myMenu():
print('\t1) - Category')
print('\t2) - Item')
print('\t3) - Serving Size')
print('\t4) - Calories')
print('\t5) - Calories from Fat')
print('\t6) - Total Fat')
print('\t7) - Cholesterol')
print('\t8) - Sodium')
print('\t9) - Carbs')
print('\t10) - Protein')
print('\t11) - Sugar')
print('\t12) - Done')
def processInput(results):
headings=['Category','Item','Serving Size','Calories','Calories
from Fat','Total Fat','Cholesterol',\
'Sodium','Carbs','Protein','Sugar']
print('The user entered choice: {0} -
{1}'.format(results,headings[results-1]))
sortData = sorted(data, key = lambda r:(int(r[results-1])), reverse
= True)
top5 = 0
print("Top 5 Items:\n")
for count in range(5):
top5+=1
print(" {}| {} {}".format(top5, sortData[count][1],
sortData[count][results-1]))
def printMenu():
while True:
try:
myMenu()
choice = int(input('Enter a number between 1 and 12. '))
if choice > 12:
print('Enter a number between 1 and 12. ')
else:
return choice
except ValueError:
print('Invalid number enter.')
def main():
choice=printMenu()
processInput(choice)
main()
This is my code and my prof. said.
Your code is working....with one change
In your processInput() function you need to determine what your
results variable is:
if "results" is greater than or equal to 0 and less then or equal
to 2:
sort WITHOUT the int: sortData = sorted(data, key = lambda
r:(r[results-1]), reverse = True)
else
if "results is greater than 5 and less than 10:
results += 4
sortData = sorted(data, key = lambda r:(int(r[results-1])), reverse
= True)
How do I change it? by putting an if statement? in a try and except? I don't know.
#i have implemented what you have described
def processInput(results):
headings=['Category','Item','Serving Size','Calories','Calories
from Fat','Total Fat','Cholesterol',\
'Sodium','Carbs','Protein','Sugar']
print('The user entered choice: {0} -
{1}'.format(results,headings[results-1]))
if results>=0 and results<=2:
sortData = sorted(data, key = lambda r:((r[results-1])), reverse =
True)
elif results>5 and results<10:
results += 4
sortData = sorted(data, key = lambda r:(int(r[results-1])), reverse
= True)
top5 = 0
print("Top 5 Items:\n")
for count in range(5):
top5+=1
print(" {}| {} {}".format(top5, sortData[count][1],
sortData[count][results-1]))