我的代码告诉我我需要浮动,但是当我使用浮动时,它告诉我“无法将'float'对象隐式转换为str“

时间:2019-09-29 02:22:14

标签: python

我不知道我在做什么错,请帮忙:(
输出预期:

预期:

  1. 商品名称:
  2. 购买数量:
  3. 每件价格:
  4. 您想输入其他项目吗?键入“ c”表示继续,或者键入“ q”退出:
  5. 1杯牛奶@ $ 2.99到$ 2.99
  6. 总计:2.99美元

这是我的代码:

The task is broken down into three sections.
Section 1 - User Input
Section 2 - loop through the grocery list
Section 3 - provide output to the console

#Task: Create the empty data structure
grocery_item = {}
grocery_history = []

#Variable used to check if the while loop condition is met
stop = 'c'

while stop == 'c':

#Accept input of the name of the grocery item purchased.
  item_name = input("Item name:\n")

#Accept input of the quantity of the grocery item purchased.
  quantity = input("Quantity purchased:\n")   

#Accept input of the cost of the grocery item input (this is a per-item cost).
  cost = input("Price per item:\n")

    #Using the update function to create a dictionary entry which contains the name, number and price entered by the user.
  grocery_item['name'] = item_name        
  grocery_item['number'] = quantity 
  grocery_item['price'] = float(cost)
  grocery_item = {'name':item_name,'number':quantity,'price':cost}
    #Add the grocery_item to the grocery_history list using the append function
  grocery_history.append(grocery_item)

    #Accept input from the user asking if they have finished entering grocery items.
  stop = input("Would you like to enter another item?\n Type 'c' for continue or 'q' to quit:\n")

# Define variable to hold grand total called 'grand_total'
  grand_total = 0
#Define a 'for' loop.  

for items in range(0, len(grocery_history)):

  #Calculate the total cost for the grocery_item.
    item_total = int(grocery_history[items].get('number')) * float(grocery_history[items].get('price'))
  #Add the item_total to the grand_total
    grand_total = grand_total + float(item_total)
  #Output the information for the grocery item to match this example:
  #2 apple  @   $1.49   ea  $2.98

    print(grocery_history[items]['number'] + ' ' + str(grocery_history[items]['name']) + ' @ $' + str('%.2f' % grocery_history[items]['price']) + ' ea $' + str('%.2f' % item_total))


  #Set the item_total equal to 0
item_total = 0
#Print the grand total

print(str('Grand total: $%.2f' % grand_total))

'''

2 个答案:

答案 0 :(得分:2)

问题在于以下四行:

grocery_item['name'] = item_name        
grocery_item['number'] = quantity 
grocery_item['price'] = float(cost)
grocery_item = {'name':item_name,'number':quantity,'price':cost}

最后一行结束时将撤消前三行,并将grocery_item['price']分配为 string 值,而不是 float 。这会导致最终错误,尝试使用%.2f将值打印为浮点数。

为什么第四行甚至在那里?只需将其删除。

答案 1 :(得分:1)

容易修复,只需将grocery_historystr包裹

print(str(grocery_history[items]['number']) + ' ' + str(grocery_history[items]['name']) + ' @ $' + str('%.2f' % grocery_history[items]['price']) + ' ea $' + str('%.2f' % item_total)) 

此外,当您尝试向字符串中添加浮点数/整数时,也会抛出该错误,因为python希望您先 明确地将浮点数/整数转换为字符串,方法是使用{ {1}}功能。

更新

使用f字符串进行打印更清洁:

str

现在,您甚至不需要在花车上使用print(f"{grocery_history[items]['number']} {grocery_history[items]['name']} @ $ {grocery_history[items]['price']} ea ${item_total}") 。这仅适用于python> = 3.6