Question

Make a .py file called fruitStorage.py. The script should asks users for a fruit/vegetable and the...


Make a .py file called fruitStorage.py. The script should asks users for a fruit/vegetable and the count of those items that they have, or “quit” to quit. Until the user enters quit, store the fruit and their count in a list of tuples, of the form:


[(tomato, 5), (blueberry, 10), ….]

Duplicates should add the count to the already stored count.

When the user enters quit, print a report of the user’s input, with the following general appearance:

Produce, Count
tomato, 5
blueberry, 10



Average count per produce item: #
Total # of produce: #

Where total is the sum of counts and the average is the average count of produce per produce.


Make an edit of the first .py, called fruitDictionary. Perform the same task as above, with the same output. The exact approach is up to you. In a comment in the .py file, say which of the two approaches you prefer and why.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code,indentation(as HomeworkLib editor doesn't support indentation in its editor) and test cases are provided below..please comment for any queries and don't forget to thumbs up if helped!

code:

#fruitStorage.py

ftupls=list()

flag=True

while(flag):

print("1.add a fruit/vegetable\n2.quit")

inp=int(input())

if(inp==1):

print("enter fruit/vegetable name")

fname=input()

print("enter fruit/vegetable count")

fcount=int(input())

ls=[item for item in ftupls if item[0] == fname]

if(ls==[]):

ftupls.append((fname,fcount))

else:

for i,(fruit, count) in enumerate(ftupls):

if(fruit==fname):

ftupls[i] = (fruit, count+fcount)

if(inp==2):

print("Produce,Count")

total=0

for tp in ftupls:

print("{},{}".format(tp[0],tp[1]))

total+=tp[1]

avg=total/len(ftupls)

print("Average count per produce item: {}".format(avg))

print("Total of produce: {}".format(total))

flag=False

indentation:

2 ftupls-list() 3 flag True while(flag): print(1.add a fruit/vegetable\n2.quit) inp-int(input()) if(inp-1): print(enter fruit/vegetable name) fname-input() print(enter fruit/vegetable count fcount-int(input()) 10 12 13 ls [item for item in ftupls if item[0] ifls []): fname ] ftupls.append((fname, fcount)) 16 else: for i,(fruit, count) in enumerate(ftupls): if(fruit-fname): 18 19 20 21 ftupls[i]- (fruit, count+fcount) if(inp-2): print( Produce, Count) total-0 for tp in ftupls: 23 2 4 25 26 print(,.format(tp[0],tp[1])) total+-tp[1] avg-total/len(ftupls) print(Average count per produce item: .format (avg)) print( Total of produce: 1.format(total)) flag-False 28 29

output:

code:

#fruitDictionary.py

fdicls=list()

flag=True

while(flag):

print("1.add a fruit/vegetable\n2.quit")

inp=int(input())

if(inp==1):

print("enter fruit/vegetable name")

fname=input()

print("enter fruit/vegetable count")

fcount=int(input())

if(any(fname in item for item in fdicls)):

for fd in fdicls:

for fruit,count in fd.items():

if(fruit==fname):

fd[fruit] = count+fcount

else:

fdicls.append({fname:fcount})

if(inp==2):

print("Produce,Count")

total=0

for fd in fdicls:

for fruit,count in fd.items():

print("{},{}".format(fruit,count))

total+=count

avg=total/len(fdicls)

print("Average count per produce item: {}".format(avg))

print("Total of produce: {}".format(total))

flag=False

indentation:

output:

#comment in any of the .py file provided this:

#i would prefer using dict for this problem as it makes accessing and manipulating values by keys easy

Add a comment
Know the answer?
Add Answer to:
Make a .py file called fruitStorage.py. The script should asks users for a fruit/vegetable and the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Python script file called hw12.py. Add your name at the top as a comment,...

    Create a Python script file called hw12.py. Add your name at the top as a comment, along with the class name and date. Ex. 1. a. Texting Shortcuts When people are texting, they use shortcuts for faster typing. Consider the following list of shortcuts: For example, the sentence "see you before class" can be written as "c u b4 class". To encode a text using these shortcuts, we need to perform a replace of the text on the left with...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT