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 to manage a user list that can be modified and saved to...

    Create a python script to manage a user list that can be modified and saved to a text file. Input text file consisting of pairs of usernames and passwords, separated by a colon (:) without any spaces User choice: (‘n’-new user account, ‘e’-edit existing user account, ‘d’- delete existing user account, ‘l’- list user accounts, ‘q’-quit) List of user accounts, error messages when appropriate Output text file consisting of pairs of username and passwords, separated by a colon (:) without...

  • We use A porgram called :"Aptana" Any ideas please Help:( 1. Write an HTML file with...

    We use A porgram called :"Aptana" Any ideas please Help:( 1. Write an HTML file with one button. The value of the button should be your name. Name the file YourFirstName YourLastName.html 5 points. 2. Write a separate JavaScript file called YourFirstName YourLastname.js in the same folder. When the button is clicked, the program should prompt the user for a string, or *** to quit. It should read the string into a variable. 10 points 3. Assume the user enters...

  • 1. Create a Python script file called Assignment_Ch06-02_yourLastName.py. (Replace yourLastName with your last name.) 2. Write...

    1. Create a Python script file called Assignment_Ch06-02_yourLastName.py. (Replace yourLastName with your last name.) 2. Write a Python program that prompts the user for a sentence, then replaces all the vowels in the sentence with an asterisk: '*' Your program should use/call your isVowel function from Assignment_Ch06-01. You can put the isVowel() function in a separate .py file, without the rest of the Ch06-01 code, and then import it. 6-01 CODE: def isVowel(x):     if x in "aeiouyAEIOUY":         return True     else:...

  • 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...

  • 1. Write a program called Numbers that a. prompts the user for a file name. b....

    1. Write a program called Numbers that a. prompts the user for a file name. b. reads that file assuming that its contents consist entirely of integers. c. prints the maximum, minimum, sum, count (number of integers in the file), and average of the numbers. For example, if the file numberinput.dat has the following content: 4 -2 18 15 31 27 Your program should produce the following output: csc% java Numbers Enter file name: numberinput.daft Maximum31 Minimum- -2 Sum -...

  • The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that...

    The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that is an ArrayList of Token objects. Tokenizer should have a private variable that is an int, and keeps track of the number of keywords encountered when parsing through a files content. In this case there will only be one keyword: public. Tokenizer should have a default constructor that initializes the ArrayList of Token objects Tokenizer should have a public method called: tokenizeFile tokenizeFile should...

  • Python Coding The script, `eparser.py`, should: 1. Open the file specified as the first argument to...

    Python Coding The script, `eparser.py`, should: 1. Open the file specified as the first argument to the script (see below) 2. Read the file one line at a time (i.e., for line in file--each line will be a separate email address) 3. Print (to the screen) a line that "interprets" the email address as described below 4. When finished, display the total number of email addresses and a count unique domain names Each email address will either be in the...

  • # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from #...

    # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from # http://www.cs.uiowa.edu/~cremer/courses/cs1210/etc/ds4/ # Save both in the same folder. # # 2. TA (aloud) and STUDENTS: Read the comments from START HERE! (just after these instructions) # to definition of anagramInfo function. Discuss any questions about what the functions should do. # # 3. TA demonstrate running anagramInfo("wordsMany.txt") on this unchanged file, to # see that it behaves reasonably despite having incomplete anagram-testing functions. #...

  • Problem 1 Recall that when the built-in function open() is called to open a file for...

    Problem 1 Recall that when the built-in function open() is called to open a file for reading, but it doesn't exist, an exception is raised. However, if the file exists, a reference to the opened file object is returned. Write a function safeOpen() that takes one parameter, filename - a string giving the pathname of the file to be opened for reading. When safeOpen() is used to open a file, a reference to the opened file object should be returned...

  • Problem 1 Recall that when the built-in function open() is called to open a file for...

    Problem 1 Recall that when the built-in function open() is called to open a file for reading, but it doesn’t exist, an exception is raised. However, if the file exists, a reference to the opened file object is returned. Write a function safeOpen() that takes one parameter, filename — a string giving the pathname of the file to be opened for reading. When safeOpen() is used to open a file, a reference to the opened file object should be returned...

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