python8day

-- coding=utf-8 --

from day_7 import Car,Electricar
my_new_car=Car(‘audi’,‘a4’,2016)
print(my_new_car.get_description_name())
my_new_car.odometer_reading=23
my_new_car.read_odometer()
my_tesla=Electricar(‘TESLA’,‘model s’,2016)
print(my_tesla.get_description_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
with open(‘C:/Users/Administrator/Desktop/无标题.txt’,encoding=‘UTF-8’) as f:
#conents=f.read()#读取整个文件
#print(conents)
# for line in f:#逐行读取
#print(line)
lines=f.readlines()
#for line in lines:
#print(line.rstrip())
pi_string=’’
for line in lines:
pi_string+=line.rstrip()
print(pi_string)
print(len(pi_string))
#写入空文件,w写入,r读取,a附加,r+读取和写入
with open(‘C:/Users/Administrator/Desktop/test.txt’,‘a’,encoding=‘UTF-8’) as f_1:
f_1.write("\nl love programing,your?")
***************************************************************************************
# 异常,try-except代码块
try:
print(5 / 0)
except ZeroDivisionError:
print(“you can`t divide by zero!”)

异常奔溃

print(“please give me two numbers,and l’ll divide them.”)
print(" Enter ‘q’ quit it.")
while True:
first_number = input("\nFirst number: “)
if first_number == ‘q’:
break
second_number = input(”\nSecond number: ")
if second_number == ‘q’:
break
answer = int(first_number) / int(second_number)
print(answer)

else代码块

print(“please give me two numbers,and l’ll divide them.”)
print(" Enter ‘q’ quit it.")
while True:
first_number = input("\nFirst number: “)
if first_number == ‘q’:
break
second_number = input(”\nSecond number: ")
try:
answer = int(first_number) / int(second_number)
except ZeroDivisionError:
print("you can’t divide by 0 ")
else:
print(answer)

处理FileNotFoundError

filename = ‘alice.txt’
try:
with open(filename) as f_objt:
contents = f_objt.read()
except FileNotFoundError:
msg = “Sorry,the file " + filename + " does not it!”
print(msg)

猜你喜欢

转载自blog.csdn.net/qq_38501057/article/details/88426104