Question

In: Computer Science

data = [] infile = open("Mac_menu.csv", "r") line = infile.readline() for line in infile: line =...

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.

Solutions

Expert Solution

#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]))


Related Solutions

Recall that (a,b)⊆R means an open interval on the real number line: (a,b)={x∈R|a<x<b}. Let ≤ be...
Recall that (a,b)⊆R means an open interval on the real number line: (a,b)={x∈R|a<x<b}. Let ≤ be the usual “less than or equal to” total order on the set A=(−2,0)∪(0,2) Consider the subset B={−1/n | n∈N, n≥1}⊆A. Determine an upper bound for B in A. Then formally prove that B has no least upper bound in A by arguing that every element of A fails the criteria in the definition of least upper bound. Note: least upper bound is an upper...
Plot the data and line of best fit in R and paste it into this document
C D 3 2 6 7 8 5 9 4 1 0 3 4 Plot the data and line of best fit in R and paste it into this document Code: Plot:
1) Show that if A is an open set in R and k ∈ R \...
1) Show that if A is an open set in R and k ∈ R \ {0}, then the set kA = {ka | a ∈ A} is open.
Is my proof that empty set is open and R is open correct?
Is that empty set is open and R is open correct? Give details Explaination.
original code: filename = "CCL-mbox-tiny.txt" with open(filename, 'r') as f: for line in f.readlines(): print(line.strip()) 1....
original code: filename = "CCL-mbox-tiny.txt" with open(filename, 'r') as f: for line in f.readlines(): print(line.strip()) 1. Now modify the code above to print the number of lines in the file, instead of printing the lines themselves. You'll need to increment a variable each time through the loop and then print it out afterwards. There should be **332** lines. 2. Next, we'll focus on only getting lines addressing `X-DSPAM-Confidence:`. We do this by including an `if` statement inside the `for` loop....
we have defined open sets in R: for any a ∈ R, there is sigma >...
we have defined open sets in R: for any a ∈ R, there is sigma > 0 such that (a − sigma, a + sigma) ⊆ A. (i) Let A and B be two open sets in R. Show that A ∩ B is open. (ii) Let {Aα}α∈I be a family of open sets in R. Show that ∪(α∈I)Aα is open. Hint: Follow the definition of open sets. Please be specific and rigorous! Thanks!
Show that any open subset of R (w. standard topology) is a countable union of open...
Show that any open subset of R (w. standard topology) is a countable union of open intervals. Please explain how to do, I only understand why it is true. What is required to fully prove this. What definitions should I be using.
Let R be the real line with the Euclidean topology. (a) Prove that R has a...
Let R be the real line with the Euclidean topology. (a) Prove that R has a countable base for its topology. (b) Prove that every open cover of R has a countable subcover.
Show that a subset of R is open if an only if it can be expressed...
Show that a subset of R is open if an only if it can be expressed as a union of open intervals
Discuss the benefits and limitations of open source R&D?
Discuss the benefits and limitations of open source R&D?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT