以下代码按预期工作:
class Item(object):
def __init__(self, unq_id, name, price, qty, measure):
self.unq_id = unq_id
self.product_name = name
self.price = price
self.qty = qty
self.measure = measure
class Cart(object):
def __init__(self):
self.content = dict()
def __format__(self, format_type):
if format_type == 'short':
return ', '.join(item.product_name for item in self.content.values())
elif format_type == 'long':
return '\n'.join(f'\t\t{item.qty:2} {item.measure:7} {item.product_name:12} @ '
f'${item.price:1.2f} ... ${item.qty * item.price:1.2f}'
for item in self.content.values())
def add(self, item):
if item.unq_id not in self.content:
self.content.update({item.unq_id: item})
return
for k, v in self.content.get(item.unq_id).items():
if k == 'unq_id':
continue
elif k == 'qty':
total_qty = v.qty + item.qty
if total_qty:
v.qty = total_qty
continue
self.remove_item(k)
else:
v[k] = item[k]
def get_total(self):
return sum([v.price * v.qty for _, v in self.content.items()])
def get_num_items(self):
return sum([v.qty for _, v in self.content.items()])
def remove_item(self, key):
self.content.pop(key)
if __name__ == '__main__':
item1 = Item(1, "Cucumbers", 1., 1, 'kg')
item2 = Item(2, "Tissues", 2., 2, 'dozen')
item3 = Item(3, "Tomatoes", 3., 5, 'pound')
item4 = Item(4, "Toothpaste", 1., 5, 'box')
cart = Cart()
cart.add(item1)
cart.add(item2)
cart.add(item3)
cart.add(item4)
print("Your cart contains: {0:short}".format(cart))
# cart.remove_item(1)
print()
print("Your cart contains: \n {0:long}".format(cart))
print()
print("The total number of items in your cart is: ", cart.get_num_items())
print()
print("The total cost of the items in your cart is: ", cart.get_total())
print()
cart.remove_item(3)
print("Your cart contains: {0:short}".format(cart))
print()
print("Your cart contains: \n {0:long}".format(cart))
print()
print("The total number of items in your cart is: ", cart.get_num_items())
print()
print("The total cost of the items in your cart is: ", cart.get_total())
我的问题是PyCharm对这段代码感到愤怒:
print("Your cart contains: \n {0:long}".format(cart))
PyCharm说我正在使用“不支持的格式字符'|' “< - 看起来像PyCharm里面的垂直条。由于一切正常,我不确定PyCharm在抱怨什么。我想知道PyCharm反对的是什么。
以上代码的输出:
Your cart contains: Cucumbers, Tissues, Tomatoes, Toothpaste
Your cart contains:
1 kg Cucumbers @ $1.00 ... $1.00
2 dozen Tissues @ $2.00 ... $4.00
5 pound Tomatoes @ $3.00 ... $15.00
5 box Toothpaste @ $1.00 ... $5.00
The total number of items in your cart is: 13
The total cost of the items in your cart is: 25.0
Your cart contains: Cucumbers, Tissues, Toothpaste
Your cart contains:
1 kg Cucumbers @ $1.00 ... $1.00
2 dozen Tissues @ $2.00 ... $4.00
5 box Toothpaste @ $1.00 ... $5.00
The total number of items in your cart is: 8
The total cost of the items in your cart is: 10.0
Process finished with exit code 0
答案 0 :(得分:2)
该竖线是小写字母L
,它在格式字符串long
的开头找到。它抱怨,因为PyCharm只识别某个字符子集(即https://docs.python.org/2/library/string.html#format-specification-mini-language),因为它不知道搜索__format__
的运算符重载。
不幸的是,我最好的建议是为PyCharm添加一个noinspection
子句来接收:
# noinspection PyStringFormat
print("Your cart contains: \n {0:long}".format(cart))