python 构建类_使用Python中的类构建餐厅菜单

python 构建类

Here, we try to use class in python to build a Menu for the restaurant. The Menu will contain the Food Item and its corresponding price. This program aims to develop an understanding of using data abstraction in general application.

在这里,我们尝试使用python中的class来为restaurant建立菜单 。 菜单将包含食品及其相应的价格。 该程序旨在发展对在一般应用中使用数据抽象的理解。

Program:

程序:

# Definig a class food, 
# which contain name and price of the food item

class Food(object):
    def __init__(self, name, price):
        self.name = name
        self.price = price
    
    def getprice(self):
        return self.price
    
    def __str__(self):
        return self.name + ' : ' + str(self.getprice())
  
# Defining a function for building a Menu 
# which generates list of Food    
def buildmenu(names, costs):
    menu = []
    for i in range(len(names)):
        menu.append(Food(names[i], costs[i]))
    return menu

# items
names = ['Coffee', 'Tea', 'Pizza', 'Burger', 'Fries', 'Apple', 'Donut', 'Cake']

# prices
costs = [250, 150, 180, 70, 65, 55, 120, 350]

# building food menu
Foods = buildmenu(names, costs)

n = 1
for el in Foods:
    print(n,'. ', el)
    n = n + 1

Output

输出量

1 .  Coffee : 250
2 .  Tea : 150
3 .  Pizza : 180
4 .  Burger : 70
5 .  Fries : 65
6 .  Apple : 55
7 .  Donut : 120
8 .  Cake : 350


翻译自: https://www.includehelp.com/python/building-restaurant-menu-using-class.aspx

python 构建类

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值