Python Forum
Python Coding Homework Help "Grocery Store Receipt"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Coding Homework Help "Grocery Store Receipt"
#1
I was assigned to create a "receipt" type output for homework using 2 dictionaries, one for the quantity of items and the item, and the other for the price of the item and the item. We have to use external text documents put into the dictionaries. I am getting the dictionaries to work and output what they are supposed to, however, I am having trouble trying to output these onto the console, since they won't print at all. The issue I am having is the part where you process the grocery list, so if you could help me that would be great!

    
grocery_dictionary = {}
gro_inv_file = open("groc_test2.txt", "r")

for line in gro_inv_file:
    line = line.strip()
    product = line.split()
    #print(product)
    key = product[0]
    value = float(product[1])
    #print(key,value)
    grocery_dictionary[key] = value

print(grocery_dictionary)
#gro_inv_file.close()


#=====================================================Second Dictionary=========

grocery_dictionary2 = {}
gro_inv_file2 = open("groc_list_test.txt", "r")

for line2 in gro_inv_file2:
    line2 = line2.strip()
    product2 = line2.split()
    #print(product2)
    key2 = product2[1]
    value2 = product2[0]
    #print(value2,number)
    grocery_dictionary2[key2] = value2

print(grocery_dictionary2)
#gro_inv_file2.close()


#====================================== Process our grocery list =============

print("{:6s} {:7s} {:2s} {:6s} {:2s} {:5s}".format("QTY","ITEM", "@", "PPU", "=", "Total")) # Top of reciept

print(grocery_dictionary2[key2])


#
#final_dictionary = {}
#
#running_total = 0
#
#for item in final_dictionary:
#   # print(item)
#    quantity = grocery_dictionary2[value2]
#    grocery_item = grocery_dictionary[key]
#    unit_price = float(grocery_dictionary[value])
#    
#    extension = quantity*unit_price
#    
#    
#    print("{:3d} {:^10} @  {:4.2f}   = {:6.2f}".format(quantity, grocery_item, unit_price, extension))
#    running_total += extension
#    
#    
#print("Total due: ", running_total)
Reply
#2
What do you mean by "won't print at all?" Do you get an error?

PS: grocery_dictionary1/grocery_dictionary2 are awful names. price_dictionary and quantity_dictionary are much better. Some people would even use prices and quantities.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
It prints the "top of receipt" (the list), however the part thats commented out ("final dictionary") will not print (when it isn't commented out). I think it is because I pulled the data from the other two dictionaries incorrectly, but I'm not exactly sure. About the names, sorry, i'm still sort of new to coding.
Reply
#4
it's because final_dictionary is empty, i.e. you iterate over empty dict. Also note that value, value2 and key retain their last value from the loop when you iterate over second file.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HELP in python homework makashito 4 3,913 Oct-12-2021, 10:12 AM
Last Post: buran
  CyperSecurity Using Python HomeWork ward1995 1 1,958 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 2,301 May-09-2021, 11:19 AM
Last Post: Yoriz
  Homework with python Johnsonmfw 1 1,688 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,973 May-15-2020, 04:57 PM
Last Post: snippsat
  Python Homework Help *Urgent GS31 2 2,581 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Python Homework Question OrcDroid123 1 2,368 Sep-01-2019, 08:44 AM
Last Post: buran
  Python homework / functions sunhyunshine 1 2,451 May-11-2019, 05:37 PM
Last Post: MrTheOne
  python homework help ASAP gk34332 1 2,970 Mar-13-2019, 07:27 PM
Last Post: ichabod801
  Help!! Order form and receipt code beginner1996 2 3,276 Mar-07-2019, 03:22 PM
Last Post: beginner1996

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020