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

猜你喜欢

转载自blog.csdn.net/weixin_38533133/article/details/79821533
今日推荐