高级编程技术 Python 第五周作业

第九章:类

#9-1

class Restaurant():
    
    def __init__(self, name, cuisine_type):
        self.name = name
        self.type = cuisine_type
    
    def describe_restaurant(self):
        print('Restaurant Name: '+self.name)
        print('Cuisine Type: '+self.type)
    
    def open_restaurant(self):
        print(self.name+' is open now.')

restaurant = Restaurant('McDonald', 'fastfood')
print(restaurant.name)
print(restaurant.type)
restaurant.describe_restaurant()
restaurant.open_restaurant() 

#9-6

class Restaurant():
    
    def __init__(self, name, cuisine_type):
        self.name = name
        self.type = cuisine_type
    
    def describe_restaurant(self):
        print('Restaurant Name: '+self.name)
        print('Cuisine Type: '+self.type)
    
    def open_restaurant(self):
        print(self.name+' is open now.')

class IceCreamStand(Restaurant):
    
    def __init__(self, name, cuisine_type, flavor):
        super().__init__(name, cuisine_type)
        self.flavor = flavor
        
    def print_flavor(self):
        print('Flavors:')
        for item in self.flavor:
            print('    '+item)
    
stand = IceCreamStand('Haagen-Dazs', 'Ice cream stand', ['Chocolate', 'Strawberry', 'Mango', 'Macha'])
stand.describe_restaurant()
stand.print_flavor()

#9-10

restaurant.py

class Restaurant():
    
    def __init__(self, name, cuisine_type):
        self.name = name
        self.type = cuisine_type
    
    def describe_restaurant(self):
        print('Restaurant Name: '+self.name)
        print('Cuisine Type: '+self.type)
    
    def open_restaurant(self):
        print(self.name+' is open now.')

main.py

from restaurant import Restaurant

restaurant = Restaurant('McDonald', 'fastfood')
print(restaurant.name)
print(restaurant.type)
restaurant.describe_restaurant()
restaurant.open_restaurant() 

第十章:文件和异常

#10-4

filename = 'guests.txt'

with open(filename, 'w') as file_object:
    have_next = True
    while (have_next):
        name = input('Please input yout name:')
        print('Hello, '+name.title())
        file_object.write(name+'\n')
        temp = input('Are you the last one? (y/n) ')
        if (temp == 'y'):
            have_next = False

#10-13

import json

def get_stored_username():
    
    filename = 'username.json'
    try:
        with open(filename) as f_obj:
            username = json.load(f_obj)
    except FileNotFoundError:
        return None
    else:
        return username

def greet_user():
    
    username = input('Please input your name: ')
    stored_username = get_stored_username()
    if (username == stored_username):
        print('Welcome back '+username+'!')
    else:
        filename = 'username.json'
        with open(filename, 'w') as f_obj:
            json.dump(username, f_obj)
            print('We\'ll remember you when you come back, '+username+'!')
            
greet_user()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值