我目前正在开发一个用户可以订购比萨饼的程序。我对从字典中获取特定值并将其放入列表的最佳方法存在一些问题,我最终可以使用该列表向客户提供最终的总数
我的问题是:如何获取用户的输入,将其与“键”匹配,并将“值”附加到一个列表中,以便以后用于用户的最终合计。
这是我目前拥有的字典:
sizePrice = {
'small': 9.69,
'large': 12.29,
'extra large': 13.79,
'party size': 26.49
}
我写过一个例子:
class PizzaSize():
"""" Takes the customers size requested and returns total cost of that pizza size"""
sizePrice = {
'small': 9.69,
'large': 12.29,
'extra large': 13.79,
'party size': 26.49
}
ordered_size= []
def size(self):
self.size_wanted = input("What size pizza would you like? ")
self.size_wanted = self.size_wanted.title()
customer_size = self.size_price[self.size_wanted]
odered_size.append(customer_size)
order = PizzaSize()
order.size()
我知道上面的代码是不正确的,但是,我正在寻找任何能让我走上正确方向的东西
虽然没有经过彻底的测试,但这里有一种方法可以通过解释思考过程的注释来实现:(注意,您可以用更少的代码实现同样的效果,但是使用简单的对象和明确定义的属性编写代码可以更容易地维护更大的应用程序,因此是一个好习惯)
让我知道如果你想进一步澄清,很乐意帮助
根据要求进行澄清(查看注释以了解解释):
如果要将while循环放入函数中,可以执行以下操作:
相关问题 更多 >
编程相关推荐