Python基础:异常

#1.面向对象的三大特性:

#封装,继承,多态



#2.闭包的形成要件

#1,内部函数调用外部函数的变量

#2,外部函数包含内部函数

#3,外部函数返回内部函数的函数名



# #写一个闭包和一个装饰器

# #闭包:

def bibao():

    a=100

    def neibu(b):

        c=a*b

        return c

    return neibu

a=bibao()

print(a(3))



#装饰器
import time
def a(f):
   
def neibu(cs):
        start=time.time()
        f(cs)
        end=time.time()
       
print(end-start)
   
return neibu
@a
def jisuan(a):
    c=a+
3
   
print(c)
jisuan(
1)

 




#3.变量作用域的查找顺序
#L:local局部作用域
#E:encolse嵌套作用域
#G:global全局
#B:built-in:内置

a=1
b=2
c=3
def waiceng():
    b=
20
   
c=30
   
def neiceng():
        c=
300
       
print(a)
       
print(b)
       
print(c)
       
print(max)
    neiceng()
waiceng()

 

 




#5.输入任意两个数。输出 这两个数中间所有的质数,
#并求出这些质数的和以及这些质数中以3结尾的个数


a=int(input("请输入第一个数"))
b=
int(input("请输入第二个数"))
i=a
num=
0
numb=0
while i <=b:
    j=
2
   
while j<i:
       
if i%j==0:
           
print("%d不是质数"%i)
           
break
        else
:
            j+=
1
   
else:
       
print(i,"是质数")
        num+=i
       
if i%10==3:
            numb+=
1
   
i+=1
print("质数的和是",num,"以3结尾的有%d个"%numb)

 

 

 

#异常
a=[1,2]
try:
   
print(a[3])
except:
   
print("出现错误")
print("程序到这了")

 

 

 

 

a=[1,2]

try:

    print(a[3])

except IndexError as e:

    print("出现错误",e)

print("程序到这了")

 

 

 

 

class A():

    pass

try:

    print(A.x)

except AttributeError as x:

    print(x)

 

 

 

 

a={"name":"张三"}

try:

    print(a["age"])

except KeyError as x:

    print(x)

 

 

 

 

a=[1,2,3,4,5]

b=iter(a)

try:

    while True:

        print(next(b))

except StopIteration as n:

    print(n)

 

 

 

 

a=[1,2,]

try:

    print(a[2])#只能接收第一个异常,出现异常后异常语句后面的代码不执行,直接跳到except

    print(1/0)

except (ZeroDivisionError,IndexError) as e:

    print(e)

 

 

 

 

try:

    print(33333)

except:

    print(44444)

else:

    print("我没错")

 

 

 

 

a=[1,2]

try:

    print(a[2])

    print("x")

    print(1/0)

    print("y")

except:#只写except可以捕捉所有种类的错误,但是只能捕捉第一个错

    print("有错误")

else:

    print("我没错")

 

 

 

 

try:

    print(12)

    print(233)

    print(2/0)

except:

    print(345)

finally:#无论是否出现异常,程序都要执行

    print("结束")

 

 

 

 

# f=open("w.txt","w")

# f.close()

try:

    f=open("w.txt","r+")

    f.write("123")

except:

    print("出错了")

finally:

    print("关闭文件")

    f.close()

 

 

 

 

with open("w.txt","r") as f:#with open自带close

    x=f.read()

    print(x)

 

 

 

 

try:

    a=10

    raise IndexError("错了")#抛出异常

    print(123)#这句话不会执行

except IndexError as b:

    print("捕捉成功",b)

 

 

 

 

class MyError(Exception):

    def __init__(self,mye):

        self.mye=mye

    def __str__(self):

        return str(self.mye+"我其实没错")

try:

    raise MyError("我错了")

except MyError as e:

    print(e)

 

 

 

 

class Student():

    def __init__(self,name,age,sex,score):

        self.name=name

        self.age=age

        self.sex=sex

        self.score=score

a=Student("张三",15,"男",99)

print(dir(a))

print(a.name,a.age,a.sex,a.score)

 

 

 

 

class Student():

    address="朝阳区"

    def __init__(self,name):

        self.name=name

xh=Student("小红")

print(xh.name,xh.address)

xm=Student("小明")

print(xm.name,xm.address)

xh.address="海淀区"

print(xh.name,xh.address)

# delattr(xh,"address")

del xh.address

print(xh.name,xh.address)

 

 

 

 

class A():

    def hehe(self):

        print(self.name)

class B(A):

    def __init__(self,name):

        self.name=name

a=B("张三")

a.hehe()

 

 

 

 

class A():

    def __init__(self,name):

        self.name=name

def haha():

    print("你好")

a=A("张三")

a.haha1=haha

a.haha1()

 

 

 

 

class A():

    money=10000

    def __init__(self,name):

        self.name=name

    @classmethod

    def clsm(cls):

        print(cls.money,A.money)

A.clsm()

a=A("张三")

a.clsm()

print(isinstance(a,A))

 

 

 

 

class A():

    def __new__(cls, *args, **kwargs):

        if not hasattr(cls,"xiaogou"):

            cls.xiaogou=object.__new__(cls)

        return cls.xiaogou

    def __init__(self,name):

        self.name=name

a=A("zs")

b=A("ls")

c=A("ww")

print(a==b==c)

print(a is b is c)

print(a.name,b.name,c.name)

 

 

 

 

import os

ml=input("请输入目录")

a=ml.split("\\")

print(a)

for i in a:

    bool=os.path.exists(i)

    if bool==True:

        os.chdir(i)

    else:

        os.mkdir(i)

        os.chdir(i)

 

 

 

 

import os

ml=input("请输入目录")

b=ml.split("\\")

p=os.getcwd()

for dange in b:

    # p=p+"\\"+dange

    p=os.path.join(p,dange)

    if not os.path.exists(p):

        os.mkdir(p,0o777)

    print(p)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值