Python编程从入门到实践的第三天

#-*- coding = utf-8 -*-

#今天是12月24号了,天气阴,不是太好,这是我看Python编程从入门到实践的第三天,现在是上午,我是皮卡丘,这是我敲的第八章的代码

#第八章练习题1

#import three_test#调用该模块

#import three_test as test

#from three_test import *#调用该模块的全部函数

#from three_test import car_info

from three_test import car_info as make_car

#from three_test import User#调用该模块中定义的类

def greet_user():

'''显示简单的问候语'''

print('Hello')

greet_user()

def make_shirt(size='big',font='I Love Python.'):

'''概要的说明T恤的尺码和字样'''

print('T恤的尺码是:'+size+',它上面的字样是:'+font)

make_shirt()

make_shirt('middle')

make_shirt(font='Hello World!')

def city_country(city,country):

print(city.title()+','+country.title())

city_country('zhengzhou','henan')

def make_album(name,cd_name,num=''):

singer={'name':name,'cd_name':cd_name}

if num:

singer['num']=num

print(singer)

return singer

make_album('zhang','lo',3)

#第八章练习题2

def show_magicians(magicians):

'''打印魔法师的信息'''

for magician in magicians:

print(magician)

magician_name = ['zhang','wang','li','haha']

show_magicians(magician_name)

def make_great(magicians):

'''修改魔法师的信息'''

for i in range(len(magicians)):

magicians[i]='the Great '+magicians[i].title()

return magicians

#下面两行是对原列表进行修改

# make_great(magician_name)

# show_magicians(magician_name)

#通过传递列表的副本,不改变列表本身

magician_new=make_great(magician_name[:])

show_magicians(magician_new)

show_magicians(magician_name)

#第八章练习题3

def build_profile(first,last,**user_info):#**user_info是任意数量的关键字实参

'''创建一个字典,其中包含我们知道的有关用户的一切'''

profile={}#必须要先定义它才能使用profile字典

profile['first_name']=first.title()

profile['last_name']=last.title()

for key,value in user_info.items():

profile[key]=value

return profile

user=build_profile('zhang','xue',age=8,location='Beijing',school='Qinghua University')

print(user)

def sandwich_topping(toppings):

print('顾客需要的三明治的食材有:')

for topping in toppings:

print('-'+topping)

sandwich = ['肉','火腿肠','鸡蛋','沙拉酱','面包']

sandwich_topping(sandwich)

def car_info(zzshang,xhao,**infos):

info = {}

info['制造商']=zzshang

info['型号']=xhao

for key,value in infos.items():

info[key] = value

return info

# def car_info(zzshang,xhao,**infos):

# info = {}

# info['制造商']=zzshang

# info['型号']=xhao

# for key,value in infos.items():

# info[key] = value

# return info

#使用调用其他模块(import module_name)

# car=three_test.car_info('subaru','outback',color='blue',tow_package = True)

# print(car)

#调用其他模块的函数(from module_name import function_name)

car=make_car('subaru','outback',color='blue',tow_package = True)

print(car)

#今天是12月24号了,天气阴,不是太好,现在是下午,我是皮卡丘,这是我敲的第九章的代码

#第九章练习题1

class Restaurant():

def __init__(self,restaurant_name,cuisine_type):

self.restaurant_name = restaurant_name

self.cuisine_type = cuisine_type

def describe_restaurant(self):

print('餐厅的名字是:'+self.restaurant_name)

print('它是一个'+self.cuisine_type)

def open_restaurant(self):

print('该餐馆正在营业。')

restaurant = Restaurant('必胜客','中餐厅')

restaurant.describe_restaurant()

restaurant.open_restaurant()

class User():

def __init__(self,first_name,last_name,age,location):

self.first_name = first_name

self.last_name = last_name

self.age = age

self.location = location

# for key,value in info.items():

# self.key = value

# print(key,self.key)

def describe_user(self):

print('用户的名字是:'+self.first_name.title()+' '+self.last_name.title())

print('年龄是:'+str(self.age)+'\n居住地点是:'+self.location)

def greet_user(self):

print(self.first_name.title()+' '+self.last_name+'您好!')

user_one = User('zhang','san',age=18,location='Beijing')

user_one.describe_user()

user_one.greet_user()

user_two = User('liu','ye',age=46,location='France')

user_two.describe_user()

user_two.greet_user()

class Car():

def __init__(self,make,model,year):

self.make = make

self.model = model

self.year = year

self.odometer = 0#设置汽车的里程数,若设置了初始值,就无需包含为它提供初始值的形参。

def get_describe_car(self):

long_name = str(self.year)+'  '+self.model+'  '+self.make

return long_name.title()

def update_odometer(self,miles):

if miles >= self.odometer:

self.odometer = miles

else:

print('禁止将里程数回调。')

def read_odometer(self):

print('汽车的里程数:'+str(self.odometer))

car_one = Car('audi','a4',2016)

print(car_one.get_describe_car())

car_one.update_odometer(25)

car_one.read_odometer()

car_one.update_odometer(10)

car_one.read_odometer()

#第九章练习题2

#9-4就餐人数

class Restaurant1():

def __init__(self,restaurant_name,cuisine_type):

self.restaurant_name = restaurant_name

self.cuisine_type = cuisine_type

self.number_serverd = 0

def describe_restaurant(self):

print('餐厅的名字是:'+self.restaurant_name)

print('它是一个'+self.cuisine_type)

def open_restaurant(self):

print('该餐馆正在营业。')

def read_serverd(self):

print('目前正在等待的客人有:'+str(self.number_serverd)+'位。')

def set_number_served(self,num):

self.number_serverd = num

def increament_number_serverd(self,num):

self.number_serverd += num

rest_one = Restaurant1('肯德基','快餐店')

rest_one.describe_restaurant()

rest_one.open_restaurant()

rest_one.set_number_served(10)

rest_one.read_serverd()

rest_one.increament_number_serverd(5)

rest_one.read_serverd()

#9-5尝试登陆次数

class User1():

def __init__(self,first_name,last_name,age,location):

self.first_name = first_name

self.last_name = last_name

self.age = age

self.location = location

self.login_attempts = 0

# for key,value in info.items():

# self.key = value

# print(key,self.key)

def describe_user(self):

print('用户的名字是:'+self.first_name.title()+' '+self.last_name.title())

print('年龄是:'+str(self.age)+'\n居住地点是:'+self.location)

def greet_user(self):

print(self.first_name.title()+' '+self.last_name+'您好!')

def increment_login_attempts(self):

self.login_attempts += 1

def reset_login_attempts(self):

self.login_attempts = 0

def read_login_attempts(self):

print('尝试登陆的次数:'+str(self.login_attempts))

user1_one = User1('wang','pa',50,'南京')

user1_one.describe_user()

user1_one.greet_user()

for i in range(10):

user1_one.increment_login_attempts()

user1_one.read_login_attempts()

user1_one.reset_login_attempts()

if user1_one.login_attempts == 0:

print('测试成功。'+str(user1_one.login_attempts))

#书上的电动汽车继承父类汽车

class Battery():

def __init__(self,battery_size=70):

self.battery_size = battery_size

def describe_battery(self):

print('This car has a '+str(self.battery_size)+' kWh battery')

def get_range(self):

if self.battery_size <= 70:

range = 200

else:

range = 250

message = 'This car can go approximately '+str(range)+'miles on a full charge.'

print(message)

def update_battery(self):

if self.battery_size != 85:

self.battery_size = 85

class ElectricCar(Car):

def __init__(self,make,model,year):

super().__init__(make,model,year)#将父类和子类关联起来,使子类包含父类的所有属性

# self.battery = 70

self.battery = Battery()

# def describe_battery(self):

# print('This car has a '+str(self.battery)+' kWh battery')

my_tesla = ElectricCar('tesla','model s',2016)

print(my_tesla.get_describe_car())

# my_tesla.describe_battery()

my_tesla.battery.describe_battery()

my_tesla.battery.get_range()

my_tesla.battery.update_battery()

my_tesla.battery.describe_battery()

my_tesla.battery.get_range()

# my_xinche = ElectricCar('xinche','model p',2018)

# print(my_xinche.get_describe_car())

#第九章练习题3

#9-6冰淇凌小店

class IceCreamStand(Restaurant1):

def __init__(self,restaurant_name,cuisine_type,flavors):

super().__init__(restaurant_name,cuisine_type)#没有self

self.flavors = []

for f in flavors:

self.flavors.append(f)

def describe_flavor(self):

for flavor in self.flavors:

print('你喜欢的冰淇凌口味是:'+flavor)

ice_one = IceCreamStand('蜜雪冰城','冷饮店',['巧克力味','草莓味','牛奶味','奥利奥味'])

ice_one.describe_flavor()

#9-7管理员

class Admin(User):

def __init__(self,first_name,last_name,age,location):

super().__init__(first_name,last_name,age,location)

self.privileges = 'can ban user'

def get_privileges(self,privilege):

self.privileges = privilege

def show_privileges(self):

print('The Admin has a privility that '+self.privileges+'.')

admin_one = Admin('xu','sheng',20,'Nanjing')

admin_one.show_privileges()

admin_one.get_privileges('can delete post')

admin_one.show_privileges()

#9-8权限

class Privileges():

def __init__(self):

self.privity=['can add post','can delete post','can ban user']

def make_privileges(self,privilege):

self.privity = privilege

def show_privileges(self):

print('The Admin has a privility that ',end='')

print(self.privity)

class Admin1(User):

def __init__(self,first_name,last_name,age,location):

super().__init__(first_name,last_name,age,location)

self.privileges = Privileges()

ad1_one = Admin1('zhang','ka',25,'American')

ad1_one.describe_user()

ad1_one.privileges.make_privileges('can add post')

ad1_one.privileges.show_privileges()

你可能感兴趣的:(Python编程从入门到实践的第三天)