[英]How to print specific value in a dictionary without the brackets based on user choice
[英]How to print a dictionary with multiple values without brackets?
到目前为止我有这个:
class Wine:
def __init__(self, key):
a = {}
value = []
a.setdefault(key, [])
def addYear(self, value):
a[key].insert(1, value)
def addProd(self, value):
a[key].insert(2, value)
def addCountry(self, value):
a[key].insert(3, value)
def addPrice(self, value):
a[key].insert(4, value)
def __str__(self):
for key, value in a.items():
value = ', '.join(map(str, value))
print("{}, {}".format(key, value))
b = Wine("Bread and Butter Pinot Noir")
b.addYear("2017")
b.addProd("Sonoma County")
b.addCountry("USA")
b.addPrice("30 USD")
b.__str__()
我希望 output 看起来像这样:
面包和黄油黑皮诺,2017 年,美国索诺玛县,30 美元
但它说“名称'a'未定义”
我认为您不太了解类,但您仍然需要使用 self.attribute 来使用 class 函数中的任何属性,这是一个代码,它将为您提供所需的 output
class Wine:
def __init__(self, key):
self.a = {}
self.key = key
self.value = []
self.a.setdefault(key, [])
def addYear(self, value):
self.a[self.key].insert(1, value)
def addProd(self, value):
self.a[self.key].insert(2, value)
def addCountry(self, value):
self.a[self.key].insert(3, value)
def addPrice(self, value):
self.a[self.key].insert(4, value)
def __str__(self):
for key, value in self.a.items():
value = ', '.join(map(str, value))
print("{}, {}".format(key, value))
b = Wine("Bread and Butter Pinot Noir")
b.addYear("2017")
b.addProd("Sonoma County")
b.addCountry("USA")
b.addPrice("30 USD")
b.__str__()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.