python中的函数和语法的困难

时间:2018-02-04 18:31:16

标签: python

我已经编写了一个家庭作业的程序,它应该作为一个模拟的购物清单,它将计算物品的成本。

grocery_item = {}
grocery_history=[{'name': 'milk', 'number': int(1), 'price': float(2.99)},
{'name': 'eggs', 'number': 2, 'price': 3.99},
{'name': 'onions', 'number': 4, 'price': 0.79}]


stop = 'go'
item_name = "Item name"
quantity = "Quantity purchased"
cost = "Price per item" 
print ("Item name:")  
print ("Quantity purchased:")
print ("Price per item:")
cont = 'c'

while cont != 'q':      
  item_name = "milk"
  quantity = "Quantity purchased"
  quantity = 2
  cost = "Price per item"
  cost = 2.99
  grocery_history.append(item_name)
  grocery_history.append(quantity)
  grocery_history.append(cost)
  grocery_item['name'] = item_name
  grocery_item['number'] = quantity
  grocery_item['price'] = cost
  print("Would you like to enter another item?\nType 'c' for continue or 'q'     to quit:\n")
  cont = 'c'
  item_name = "eggs"
  quantity = "Quantity purchased"
  quantity = 1
  cost = "Price per item"
  cost = 3.99
  grocery_history.append(item_name)
  grocery_history.append(quantity)
  grocery_history.append(cost)
  grocery_item['name'] = item_name
  grocery_item['number'] = quantity
  grocery_item['price'] = cost
  "Would you like to enter another item?\nType 'c' for continue or 'q' to quit:\n"
  cont = 'c'  
  item_name = "onions"
  quantity = "Quantity purchased"
  quantity = 4
  cost = "Price per item"
  cost = 0.79
  "Would you like to enter another item?\nType 'c' for continue or 'q' to quit:\n"
  cont = 'q'
  grand_total = []

  number = grocery_item['number']
  price = grocery_item['price']

 for grocery_history in grocery_item:    
   item_total = number*price
   grand_total.append(item_total)
print (grocery_item['number'] + ['name'] + "@" + ['price'] + "ea" + ['item_total'])  

item_total = 0

print (grand_total)

这是我得到的错误:

print (grocery_item['number'] + ['name'] + "@" + ['price'] + "ea" + ['item_total'])
TypeError: unsupported operand type(s) for +: 'int' and 'list'

此代码存在多个问题。

  1. 我应该使用%.2f以美元形式获得价格,但我不知道如何以及我尝试过的东西无法运作。
  2. 我的print(grocery_item)语句中存在语法错误。
  3. 代码不会通过列表grocery_history运行,只是反复使用相同的输入重复。

3 个答案:

答案 0 :(得分:3)

要使用dict中的字段获取格式化的打印件,您可以使用.format(),如下所示:

print('{number} {name} @ {price:.2f} ea'.format(**grocery_item))

答案 1 :(得分:0)

您有错误,因为您要向浮点数添加字符串。 Python不知道如何做到这一点。

正如@Stephen Rauch所展示的那样,你可以用一个非常pythonic结构的一行来完成它。如果您愿意,可以使用语法 '%s\t%s\t...'%(my_variable1, my_variable2) 哪个也很好用。

答案 2 :(得分:0)

我假设您想要将项目添加到现有的grocery_history,然后计算所有杂货的总计(使用Stephen Rauch的打印答案)。

grocery_history=[{'name': 'milk', 'number': int(1), 'price': float(2.99)},
{'name': 'eggs', 'number': 2, 'price': 3.99},
{'name': 'onions', 'number': 4, 'price': 0.79}]

cont = 'c'
grand_total = 0

while cont != 'q':
    num = input("How many bottles of milk do you want to buy? ")   
    grocery_history[0]['number'] += num

    num = input("How many eggs do you want to buy? ")   
    grocery_history[1]['number'] += num

    num = input("How many onions do you want to buy? ")   
    grocery_history[2]['number'] += num

    cont = raw_input("Enter c to continue or q to quit: ")
    while cont not in ['c', 'q']:
        cont = raw_input("Wrong choice, enter your choice again: ")

for grocery_item in grocery_history:    
    item_total = grocery_item['number'] * grocery_item['price']
    grand_total += item_total
    print('${:.2f} for {number} {name} @ {price:.2f} ea'.format(item_total, **grocery_item))

print ("Grand total is ${:.2f}".format(grand_total))

出现了一些问题:

您实际上并没有要求用户输入cq,所以您只是在列表上进行了一次,最后由于cont = 'q'而退出。

您不断覆盖变量,例如:

  quantity = "Quantity purchased"
  quantity = 2

第一行指定字符串,第二行指定整数。您可以直接在dict值上操作,前提是您知道如何访问它们。

这部分:

  number = grocery_item['number']
  price = grocery_item['price']

分配了最后一次实际更新的grocery_item(鸡蛋),您使用这两个值在for循环中完成了所有计算。

下面:

  grocery_history.append(item_name)
  grocery_history.append(quantity)
  grocery_history.append(cost)

您要将字符串和数字附加到字典列表中。