python编写一个延时小程序!!!!!每隔一秒输出一次

不多说,直接上代码:

import time
class Car():

    def __init__(self,make,model,year):
        self.make = make
        self.model =model
        self.year = year
        self.odometer_read = 230#指定一个默认属性值

    def describe(self):
        message = str(self.year)+' '+self.make.upper()+' '+self.model
        return message

    # def read_odometer(self):
    #     print('The car has '+str(self.odometer_read)+' miles on it.')

    def updata_odometer(self,mileage):#通过定义一个方法来修改默认值
        if mileage >self.odometer_read:
            self.odometer_read = mileage#通过定义一个范围,来禁止回调
        else:
            print('You can not roll back an odometer!')

    def increment_odometer(self,mileage):
        active = True
        start = time.time()
        while active:
            localtime = time.asctime(time.localtime(time.time()))
            time.sleep(1)
            print(localtime)
            self.odometer_read += mileage
            print('The car has ' + str(self.odometer_read) + ' miles on it.')
            if self.odometer_read == 10000:
                active = False
                print('!!!!!!!!!!!!!!!!!!!')
                 end = time.time()
                ex_time = end - start
                print('The expend time is '+str(ex_time)+' .')

car = Car('bmw','X6',2018)
print(car.describe())
car.updata_odometer(1000)
car.increment_odometer(100)
# car.read_odometer()

你可能感兴趣的:(python编程)