python 학습: 정적 방법, 학생 정보 관리, 마술 방법

4696 단어
dic={'001': {'name':'장삼','age':18,'num':'001'},'002':{'name','age':28,'num':'002'}#class File Action():@staticmethod def writeDate(dic): f=open('data.txt','w','encoding='utf-8') for k, v in dic.items(): for k1,v1 in v.items(): f.write( (k1+":"+str(v1))) if k1==‘num’: f.write(’’) else: f.write(’,’) f.close() @staticmethod def readData( ): f=open(‘data.txt’,‘r’,encoding=“utf-8”) line=f.readline() dzd={} while len(line)>0: items=line.strip().split(’,’) zd={} for item in items: s=item.split(’:’) if s[0]‘age’: zd[s[0]]=int(s[1]) else: zd[s[0]] = s[1] if s[0]‘num’: dzd[s[1]]=zd line=f.readline() print(dzd) f.close() FileAction.writeDate(dic) FileAction.readData()
class People(): tax=0 def init(self,name,age,work,salary,energy=100): self.name=name self.age=age self.work=work self.salary=salary self.__energy=energy def working(self): People.tax+=self.salary*0.2 self.__energy=self.__energy-30 if self.__energy>30 else 0 def eat(self,money): self.__energy=self.__energy+10 if self.__energy<90 else 100 def sleep(self): self.__energy=self.__energy+60 if self.__energy<40 else 100 def say(self): print(self.name,self.age,self.work,self.salary) def get_energy(self): return self.__energy class Woman(People): def shopping(self,money): self.salary-=money return self.salary# class Man(People): def say(self): super().say()print("ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ")m=man("DD", 23, "운영 총책임자", 1000000)m.say()w=Woman("LL", 21, "개발", 100000)w.shopping(2000)w.say()
a={'001': {'name':'장삼','age':18,'address':'베이징'},'002':{'name','age':28,'address':'상하이'},'003':{'name':'장대','age':38,'address':'베이징'},'004':{'name','age':28,'address':'상하이'},'005': {'name':'장6','age':38,'address':'베이징'}class Student(): @staticmethod def write(): f=open('d.txt','w','encoding='utf-8') f.write(str(a) f.close() @staticmethod defread(): f=open('d.txt','r', encoding='utf-8')c=f.read d() f.close() return eval© Student.write() a=Student().read () while True:num=int ("수행할 작업의 번호를 입력하십시오") if num==1: xh=input ("학번호를 입력하십시오") if xh in a.keys (): print ("학번호 중복")
     else:
         zd={}
         zd["name"]=input(" ")
         zd["age"]=int(input(" "))
         zd["address"]=input(" ")
         a[xh]=zd
         print(a)
 elif num==2:
     xh = input(" ")
     if xh not in a.keys():
         print(" ")
         continue
     else:
         print(" ",a[xh])
         del a[xh]
         print(a)

 elif num==3:
    xh = input(" ")
     if xh not in a.keys():
         print(" ")
         continue
     else:
         a[xh]["name"]=input(" ")
         a[xh]["age"]=int(input(" "))
         a[xh]["address"]=input(" ")
     print(a)
 elif num==4:
     xh = input(" ")
     if xh not in a.keys():
         print(" ")
         continue
     else:
         print(a[xh])
 else:
     a=Student.write()
     print(" , ")
     break#exit(0)

class A(): count=0 def init(self,name): self.name=name A.count+=1 print ("%s 객체가 생성되었고%d 개 객체"% (self.name, A.count))
 def __del__(self):             #          
     A.count-=1
     print(" ",self.name," %d "%A.count)

a=A("목진")b=A("임동")deladelb
class A (): obj= "def new (cls, *args, *kwargs): print (초기화하기 전에 대상을 만들었습니다) return object.new(A) def init(self,n,name,cid): self.n=n self.name=name self.cid=cid defadd(self,other): print("가법 연산을 진행했다")return self.n+other.n defsub(self,other): print("감법 연산을 진행했다")return self.n-other.n def call(self, args, **kwargs): result=self.n for a in args: result=resulta for k,v in kwargs.items():# result+=v return result a=A(5) b=A(3) c=a+b d=a-b print© print(d)e=a(1,2,3,name=3,age=4)a=A(2,'666', 66666)print(A,'obj')#클래스에 지정된 속성이 있는지 판단print(A,'aoe')
class Car(): def new(cls, *args, **kwargs): if not hasattr(Car,“inst”): Car.inst=object.new(Car) return Car.inst def init(self,name,cid): print(“666”) self.name=name self.cid=cid a=Car(“BMW”,“666”) b=Car(“BC”,“777”) print(a is b) print(a.name,a.cid) print(b.name,b.cid)

좋은 웹페이지 즐겨찾기