#-*- 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编程从入门到实践第八章_Python编程从入门到实践的第三天相关推荐

  1. 儿童python编程能给孩子带来哪些好处_python编程练习好吗?会给孩子带来哪些作用?...

    通常学习别人的孩子,在以后就业方面,还有学校成绩方面都会占据很多的优势,所以说家长对孩子学习编程这一方面是相当支持和认可的.但是,python编程练习好吗?会给孩子带来哪些作用?python是一种比较 ...

  2. 慕课乐学python编程题_中国大学mooc慕课_Python编程基础_2020章节测试答案

    中国大学mooc慕课_Python编程基础_2020章节测试答案 更多相关问题 [单选] 双绕组变压器降压可获得(). [单选] 电炉和电烙铁是根据()制造而成的 [单选] 各种电流互感器产品技术数据 ...

  3. python编程入门到实践笔记习题_Python编程从入门到实践笔记——列表简介

    python编程从入门到实践笔记--列表简介 #coding=utf-8 #列表--我的理解等于c语言和java中的数组 bicycles = ["trek","cann ...

  4. python教程从入门到实践第八章_python:从入门到实践--第八章:函数

    定义:函数是带名字的代码块,用于完成具体的工作 定义函数: def greet_user():#关键字def来告诉python你要定义一个函数,这是函数定义,以冒号结尾,括号必不可少,因为可能在括号内 ...

  5. python入门与实践在线阅读_Python编程:从入门到实践(第2版)

    领取成功 您已领取成功! 您可以进入Android/iOS/Kindle平台的多看阅读客户端,刷新个人中心的已购列表,即可下载图书,享受精品阅读时光啦! - | 回复不要太快哦~ 回复内容不能为空哦 ...

  6. python入门实践19章_Python 编程从入门到实践 第19章 注销一节问题

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 现在网上的教程都是第1版的,很多代码都不能用,官网下了第2版的代码,一路用第1版教程修修改改好不容易到第19章 19.2.3 注销 这一节,现在遇到个问题 ...

  7. python快速编程入门课本第六章_python编程快速上手第六章实践项目参考code

    代码如下: 题目的意思是通过一个函数将列表的列表显示在组织良好的表格中,每列右对齐 tableData = [['apples', 'oranges', 'cherries', 'banana'], ...

  8. python第七章动手试一试_Python编程:从入门到实践的动手试一试答案(第七章)...

    #7-1 汽车租赁 car = input("What car do you need: ") print('Let me see if I can find you a' + c ...

  9. 儿童python编程能给孩子带来哪些好处_python编程入门学习对孩子成长有哪些优势?...

    python编程语言学习有意义吗? python编程语言是少儿编程培训课程中重要的组成部分,随着越来越多的孩子开始接触和学习编程,通过编程学习培养孩子良好的学习习惯和锻炼提升孩子逻辑思维能力.在pyt ...

最新文章

  1. 医学顶刊BMJ打脸谷歌:AI取代医生检测乳腺癌还远着呢
  2. Android Studio -添加你见过的最牛Log*神器*
  3. A+B/A*B求A和B
  4. 监听应用是否切到后台
  5. 冯乐乐 unity_Unity常用矩阵运算的推导补遗——切线空间
  6. 【vue2.0进阶】用axios来实现数据请求,简单易用
  7. python excel数据框_python – 熊猫数据框到Excel表
  8. 【tips】编译epic异常解决
  9. VisualStudio2005技巧集合--打造自己的CodeSnippet
  10. 为软考准备的论文!!
  11. html 倒计时 插件,jQuery倒计时插件leftTime.js
  12. iOS -- 播放本地音频文件 (Swift)
  13. 新浪云python开发_Python开发入门与实战17-新浪云部署
  14. JAVA实现UTC时间转换成北京时间
  15. 关于yuv rtp 打包_【讲堂】关于KNX编程基本规律
  16. python selenium爬虫自动登录实例
  17. python scapy 抓包_Python3下基于Scapy库完成网卡抓包解析
  18. android图片处理,让图片变成圆形
  19. 逻辑地址 与物理地址的转换
  20. uniapp+egg.js+react实现全栈笔记App

热门文章

  1. 第四代移动机器人:灵动科技V-AMR全球首发
  2. 微博回应用户被“劫持”;途牛否认破产清算;微软宣布开源 MsQuic | 极客头条...
  3. 和无用代码说再见!阿里文娱无损代码覆盖率统计方案
  4. 64% 的企业未实现智能化,5 成公司算法工程师团队规模小于 10人,AI 工程师的机遇在哪里?...
  5. 现代的 “Hello, World”,可不仅仅是几行代码而已
  6. “我们完全误解了区块链!”
  7. 写给软件工程师的 30 条建议
  8. 云原生与数据中台,企业数字化转型的“正确打开方式”
  9. 联想杨元庆:没必要做操作系统和芯片;华为Mate 20 Pro被迫退出安卓 Q Beta;GitHub推赚钱新利器 | 极客头条...
  10. 别熬夜加班了,Facebook 开源了一款代码推荐神器!| 程序员硬核评测