学习python04——《python编程: 从入门到实践》的学习

#本文章仅用于记录本人学习过程,当作笔记来用,如有侵权请及时告知,谢谢!

基础学到这里就差不多啦,接下来会进行一系列项目进行进阶学习!

P22:

#P22
创建一个表示汽车的类,存储有关汽车的信息,还有一个汇总这些信息的方法
class car():
    def __init__(self,make,model,year):
        #初始化描述汽车的属性
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0 #默认值为0

    def get_descriptive_info(self):
        #返回整洁的描述性信息
        long_name = str(self.year) + ' ' +self.make + ' ' + self.model
        return long_name

    def read_odometer(self):
        #打印一条指出汽车历程的信息
        print('My car has ' + str(self.odometer_reading) + ' miles on it')

    def update_odometer(self,mileage):
        #通过方法修改属性的值
        if mileage > self.odometer_reading:
            self.odometer_reading = mileage
            print('My car has ' + str(self.odometer_reading) + ' miles on it')
        else:
            print("You can't roll it back!") 

    def increment_odometer(self,mile):
        #print('My car has ' + str(self.odometer_reading) + ' miles on it')
        self.odometer_reading += mile
        print('My car has ' + str(self.odometer_reading) + ' miles on it')


my_newcar = car('Audi', 'a4', 2020)
print(my_newcar.get_descriptive_info())
# 2020 Audi a4
my_newcar.read_odometer()
# My car has 0 miles on it

#直接修改属性的值
my_newcar.odometer_reading = 23
my_newcar.read_odometer()
# My car has 23 miles on it

#通过方法修改属性的值
my_newcar.update_odometer(24)
# My car has 24 miles on it

my_newcar.update_odometer(22)
# You can't roll it back!

#通过方法递增
my_newcar.increment_odometer(25)
# My car has 49 miles on it

P23:

#子类的方法init()
class car():
    def __init__(self,make,model,year):
        #初始化描述汽车的属性
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0 #默认值为0

    def get_descriptive_info(self):
        #返回整洁的描述性信息
        long_name = str(self.year) + ' ' +self.make + ' ' + self.model
        return long_name

    def read_odometer(self):
        #打印一条指出汽车历程的信息
        print('My car has ' + str(self.odometer_reading) + ' miles on it')

    def update_odometer(self,mileage):
        #通过方法修改属性的值
        if mileage > self.odometer_reading:
            self.odometer_reading = mileage
            print('My car has ' + str(self.odometer_reading) + ' miles on it')
        else:
            print("You can't roll it back!") 

    def increment_odometer(self,mile):
        #print('My car has ' + str(self.odometer_reading) + ' miles on it')
        self.odometer_reading += mile
        print('My car has ' + str(self.odometer_reading) + ' miles on it')

class electircCar(car):
    #创建一个子类

    def __init__(self,make,model,year):
        super().__init__(make,model,year)
        #给子类定义属性和方法
        self.battery = 70

    def describe_battery(self):
        print('This car has a ' + str(self.battery) + '-kwh battery.')

my_tesla = electircCar('tesla', 'Model s', 2016)
print(my_tesla.get_descriptive_info())
# 2016 tesla Model s
my_tesla.describe_battery()
# This car has a 70-kwh battery.
#P24
#函数open(参数:要打开的文件名称)
#关键词with 不需要访问文件后可关闭
#方法read 读取文件全部内容

with open('D:/PYTHON学习/6.9/pi.txt') as file_object:
    # contents = file_object.read()
    # print(contents.rstrip())

    # for line in file_object:
    #     print(line.rstrip())

    lines = file_object.readlines()

    pi_string = ''
    for line in lines:
        pi_string += line.rstrip()
    print(pi_string)
    # 3.14159261577151684651516

# f = open('pi.txt','rb')
# f.read()

#遇到了bug 用相对路径显示找不到文件,但是绝对路径就可以

#写入一个空文件
file_name = 'kong.txt'
with open(file_name,'w') as file_object:
    file_object.write("I love u")

P24:

#P25
#try-except代码块
try:
    print(5/0)
except ZeroDivisionError:
    print('No, try another one.')
#No, try another one.

你可能感兴趣的:(python)