Question

The following python program totals up a bill based on how much was spent at each store.

"""
The following python program totals up a bill based on
how much was spent at each store.
"""


INPUT_FILE = 'bill.txt'


def main():


    # TODO



if __name__ == '__main__':
    main()

-------------------------------------------------------------------------------

(This needs to be in a text file)

9/2/19 [walMart] $12
9/21/19 [The Cheesecake Factory] $102
9/30/19 [Starbucks] $5
10/7/19 [WalMart] $17
10/22/19 [Starbucks] $8
10/28/19 [WalMart] $45

--------------------------------------------------------------------------

(This needs to be in a text file)

4/1/20 [Burger King] $9
4/6/20 [Pizza Hut] $82
4/12/20 [Burger King] $117
4/17/20 [McDonald's] $14
4/19/20 [Burger King] $65
4/19/20 [Joan's Fabric] $18
4/23/20 [Apple bee's] $12
4/25/20 [Pizza Hut] $29
4/29/20 [Burger King] $7

------------------------------------------------

(Output sample for bill 1)

  WalMart: $74
  The Cheesecake Factory: $102
  Starbucks: $13
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.

------------------main.py-------------

INPUT_FILE = 'bill.txt' #input file for bill

def main():
   data = dict() #create a dictionary (Store, expense) mapping
   inpfile = open(INPUT_FILE, 'r')
   for line in inpfile: #read input file line by line
       line = line.strip("\n") #strip new line character from the end of line
       start = line.find("[") #find [ and ], since store name is present in between them
       end = line.find("]")
       store = line[start+1: end] #get store name
       start = line.find("$") #find $ since expense is after $
       expense = float(line[start+1:]) #read expense
       if store in data.keys(): #if store name already in dictionary, then add expensive to its previous value
           data[store] = data[store] + expense
       else: #if store name is not in dictionary, add store to dictionary with current expense
           data[store] = expense

   for store in data.keys(): #read each key value and print on ouput
       print(store + ": $" + str(data[store]))

if __name__ == '__main__':
   main()

answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
The following python program totals up a bill based on how much was spent at each store.
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
  • This needs to be in python, however, I am having trouble with the code. Is there...

    This needs to be in python, however, I am having trouble with the code. Is there any way to use the code that I already have under the main method? If so, what would be the rest of the code to finish it? #Create main method def main(): #Create empty list list=[] #Display to screen print("Please enter a 3 x 4 array:") #Create loop for rows and have each entered number added to list for i in range(3): list.append([int(x) for...

  • Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_...

    Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_ Terminal + A sequential search of a sorted list can halt when the target is less than a given element in the list. 1 " 2 File: search.py 3 Project 11.1 4 5 Optimizes sequential search for sorted lists. 6 The complexity is O(n) in the worst case and 0(1) in the best case. 7 The average case is less than n / 2,...

  • Use of search structures! how can i make this program in python? Learning Objectives: You should...

    Use of search structures! how can i make this program in python? Learning Objectives: You should consider and program an example of using search structures where you will evaluate search trees against hash tables. Given the three text files under containing the following: - A list of students (classes Student in the distributed code) - A list of marks obtained by the students (classes Exam results in the distributed code) - A list of topics (classes Subject in the distributed...

  • Use python Start: def main(): gradeList = []    fileName = getFile() gradeList = getData(fileName) mean...

    Use python Start: def main(): gradeList = []    fileName = getFile() gradeList = getData(fileName) mean = calculateMean(gradeList) sd = calculateSD(mean, gradeList) displayHeadings(gradeList, mean, sd) curveGrades(mean, sd, gradeList)       if __name__ == "__main__": main() For this program, you will create a grade curving program by reading grades from a file, calculating the mean and standard deviation. The mean is the average value of the grades and the standard deviation measures the spread or dispersal of the numbers from the...

  • python program do not use dictionary, list only Complete the program found in assignment4.py. You may...

    python program do not use dictionary, list only Complete the program found in assignment4.py. You may not change any provided code. You may only complete the sections labeled: #YOUR CODE HERE Write a program that does the following. Reads the contents of Text from the include file, input.txt Create a dictionary of key-value pairs called index.txt Key: This represents the individual word Value: This is a list of the line number from Text where Key appeared Example: If the word...

  • Write a python code using pandas for this following problem and do not forgot to add comments in ...

    Write a python code using pandas for this following problem and do not forgot to add comments in your code. Suppose you have a text file called “President.txt” (which is shown below), you have to import this text file in code and using the data of this file, you have to create a new file called “NewPreFile.txt” where you will only show the only selected data of old “president.txt” file. In “NewPreFile.txt” file you need to show the presidents name,...

  • Infinite Spiral of Numbers (due 17 Feb 2020) HELLO, WE ARE USING PYTHON 3 TO COMPLETE...

    Infinite Spiral of Numbers (due 17 Feb 2020) HELLO, WE ARE USING PYTHON 3 TO COMPLETE THIS PROJECT!! PLEASE FOLLOW CODE SKELETON AS GIVEN AT THE END. THIS IS DUE 17TH FEB 2020, ANY AND ALL HELP WOULD BE GREATLY APPRECIATED, THANK YOU! Consider the natural numbers laid out in a square spiral, with 1 occupying the center of the spiral. The central 11 x 11 subset of that spiral is shown in the table below. 111 112 113 114...

  • Please study Chapter 7,and carefully examine the case study: "Foreign Companies in China Under Attack" please...

    Please study Chapter 7,and carefully examine the case study: "Foreign Companies in China Under Attack" please I want more 700 word respond to the following Discussion Questions. 7-12. What factors do you think are behind these events? Do some research to find out whether there have been more such problems since this writing. Is it just American companies that are being targeted? 7-13. What can firms currently operating in China, or considerating investment there, do to lessen the likelihood of...

  • Please study Chapter 7,and carefully examine the case study: "Foreign Companies in China Under Attack" please...

    Please study Chapter 7,and carefully examine the case study: "Foreign Companies in China Under Attack" please I want more 700 word respond to the following Discussion Questions. 7-12. What factors do you think are behind these events? Do some research to find out whether there have been more such problems since this writing. Is it just American companies that are being targeted? 7-13. What can firms currently operating in China, or considerating investment there, do to lessen the likelihood of...

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