0

I have to create a program that asks user to input the number of items to purchase. the program then asks the user to enter the item and price of each, give a total and provide the amount of change.

I am stuck on the part where I need to combine the lists and output the item and cost in currency format

#Checkout program

print "Welcome to the checkout counter! How many items will you be purchasing?"
number = int (raw_input ())

grocerylist = []
costs = []
for i in range(number):
    groceryitem = raw_input ("Please enter the name of product %s:" % (i+1))
    grocerylist.append(groceryitem)
    itemcost = float(raw_input ("What is the cost of %s ?" % groceryitem))
    costs.append(itemcost)

order = {}

for index in range (min(len(grocerylist), len(costs))):
    order[grocerylist[index]] = costs[index]
print ("This is your purchase") + str(order)
takendarkk
  • 3,347
  • 8
  • 25
  • 37
Holly
  • 39
  • 1
  • 6

3 Answers3

0

you can directly store it in dictionary:

#Checkout program

print "Welcome to the checkout counter! How many items will you be purchasing?"
number = int(raw_input ())
order = {}
for i in range(number):
    groceryitem = raw_input("Please enter the name of product %s:" % (i+1))
    itemcost = float(raw_input("What is the cost of %s ?" % groceryitem))
    order[groceryitem] = itemcost

print("your Purchase")
for x,y in order.items():
     print (str(x), "$"+str(y))

note: order.values() will give you price list
order.keys() will give you item list
Read about dictionary here :Dictionary

demo:

>>> order = {'cake':100.00,'Coke':15.00}
>>> for x,y in order.items():
...     print(x,"$"+str(y))
... 
cake $100.0
Coke $15.0

better using format:

>>> for x,y in enumerate(order.items()):
...     print("Item {}: {:<10} Cost ${:.2f}".format(x+1,y[0],y[1]))
... 
Item 1: cake       Cost $100.00
Item 2: Coke       Cost $15.00

Make it tabular:

print("{:<5}{:<10}{}".format("Num","Item","Cost"))
for x,y in enumerate(order.items()):
    print("{:<5}{:<10}${:.2f}".format(x+1,y[0],y[1]))
print("{:>10}{:>6}{:.2f}".format("Total","$",sum(order.values())))

Num  Item      Cost
1    cake      $100.00
2    Coke      $15.00
     Total     $115.00
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • OK. Thank you! But how do get the dictionary to print list like a receipt with the item then the price in $0.00 then the next line with the next item and price? – Holly Nov 29 '14 at 03:01
  • last one will print both item and price :) – Hackaholic Nov 29 '14 at 03:02
0
# the simple way
for index in range (min(len(grocerylist), len(costs))):
    print("Item: %s  Cost: $%5.2f " % (grocerylist[index], costs[index]))

or you can use the locale currency() function.. see Currency formatting in Python or https://docs.python.org/2/library/locale.html

Community
  • 1
  • 1
demented hedgehog
  • 7,007
  • 4
  • 42
  • 49
0

In addition to the above approaches, you can also iterate over order.keys(). And to format use the in-built string methods. Here's an example.

>>> order = {1 : 10.23, 2 : 1.0, 3 : 3.99}
>>> for key, val in order.items():
...     print "Item " + key + ": ", "$" + "{0:.2f}".format(val)
... 
Item 1: $10.23
Item 2: $1.00
Item 3: $3.99
slider
  • 12,810
  • 1
  • 26
  • 42