hw5(第五周)

# [9-1, 9-2, 9-3]
# 9-1
class Restaurant:
    def __init__(self, _name, _type):
        self.restaurant_name = _name
        self.cuisine_type = _type

    def describe_restaurant(self):
        print("Name: " + self.restaurant_name + ", Type: " + self.cuisine_type)

    def open_restaurant(self):
        print("This restaurant is open.")

# 9-3
class User:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    def describe_user(self):
        print("First Name: " + self.first_name + ", Last Name: " + self.last_name)

    def greet_user(self):
        print("Hello, " + self.first_name + " " + self.last_name + ".")

# 9-2, 9-3
if __name__ == '__main__':
    rs = []
    rs.append(Restaurant('A', '1'))
    rs.append(Restaurant('B', '2'))
    rs.append(Restaurant('C', '3'))
    us = []
    us.append(User('A', 'B'))
    us.append(User('Jonathan', 'Joestar'))
    for r in rs:
        print(r.restaurant_name + ' ' + r.cuisine_type)
        r.describe_restaurant()
        r.open_restaurant()
    for u in us:
        u.describe_user()
        u.greet_user()

# [9-6, 9-7, 9-8]
class Restaurant:
    def __init__(self, _name, _type):
        self.restaurant_name = _name
        self.cuisine_type = _type

    def describe_restaurant(self):
        print("Name: " + self.restaurant_name + ", Type: " + self.cuisine_type)

    def open_restaurant(self):
        print("This restaurant is open.")

class User:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    def describe_user(self):
        print("First Name: " + self.first_name + ", Last Name: " + self.last_name)

    def greet_user(self):
        print("Hello, " + self.first_name + " " + self.last_name + ".")

# 9-6        
class IceCreamStand(Restaurant):
    def __init__(self, _name, _type, _flavors):
        super().__init__(_name, _type)
        self.flavors = _flavors

    def print_flavors(self):
        for flavor in self.flavors:
            print(flavor)

iceCreamStand = IceCreamStand('A', '1', ['Chocolate', 'Banana'])
iceCreamStand.print_flavors()

# 9-7, 9-8
class Admin(User):
    def __init__(self, first_name, last_name, privileges):
        super().__init__(first_name, last_name)
        self.privileges = privileges

    def show_privileges(self):
        self.privileges.show_privileges()

class Privileges:
    def __init__(self, privileges):
        self.privileges = privileges

    def show_privileges(self):
        for p in self.privileges:
            print(p)

admin = Admin('Van', 'Darkholme', Privileges(['can delete post', 'can ban user']))
admin.show_privileges()

# [10-1, 10-2, 10-3, 10-4, 10-5]
# 10-1
with open('learning_python.txt', 'w') as f:
    f.write('In Python you can learn python→_→.\nIn Python you can learn py.')
with open('learning_python.txt', 'r') as f:
    print(f.read())
print('----------')
with open('learning_python.txt', 'r') as f:
    for what in f:
        print(what)
print('----------')
with open('learning_python.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        print(line)
# 10-2
string = ""
with open('learning_python.txt', 'r') as f:
    string = f.read().replace('Python', 'Groovy')
print(string)
with open('learning_python.txt', 'w') as f:
    f.write(string)
# 10-3
with open('guest.txt', 'w') as f:
    f.write(input('Input your name: '))
# 10-4
with open('guest_book.txt', 'a') as f:
    active = True
    while active:
        s = input('Input your name: ')
        if len(s):
            f.write(s + '\n')
        else:
            active = False
# 10-5
with open('why_guest_like_programming.txt', 'a') as f:
    active = True
    while active:
        s = input('Input your name: ')
        if len(s):
            f.write(s)
            w = input('Why do you like programming? ')
            f.write(' reason: ' + w + '\n')
        else:
            active = False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值