【问题标题】:Python Supermarket combine lists and formatPython Supermarket 结合列表和格式
【发布时间】:2014-11-29 02:49:07
【问题描述】:

我必须创建一个程序,要求用户输入要购买的商品数量。然后程序要求用户输入每个项目的项目和价格,给出一个总数并提供找零的数量。

我被困在需要合并列表并以货币格式输出项目和成本的部分

#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)

【问题讨论】:

    标签: python


    【解决方案1】:
    # the simple way
    for index in range (min(len(grocerylist), len(costs))):
        print("Item: %s  Cost: $%5.2f " % (grocerylist[index], costs[index]))
    

    或者你可以使用 locale currency() 函数。见Currency formatting in Pythonhttps://docs.python.org/2/library/locale.html

    【讨论】:

      【解决方案2】:

      除了上述方法之外,您还可以迭代order.keys()。并使用内置的字符串方法进行格式化。这是一个例子。

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

      【讨论】:

        【解决方案3】:

        您可以直接将其存储在字典中:

        #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))
        

        注意:order.values() 会给你价格表
        order.keys() 将为您提供项目列表
        在此处阅读字典:Dictionary

        演示:

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

        最好使用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
        

        表格化:

        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
        

        【讨论】:

        • 好的。谢谢!但是如何让字典像收据一样打印列表,然后是 0.00 美元的价格,然后是下一行的下一个项目和价格?
        • 最后一个将打印商品和价格:)
        猜你喜欢
        • 2011-01-29
        • 1970-01-01
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        • 2022-10-02
        • 1970-01-01
        • 2015-01-20
        相关资源
        最近更新 更多