Python学习 —— 阶段综合练习二,python综合练习

Python学习第三日-账号登录练习,python第一周

二零一七年一月初开头python的上学。选取python 3.6。

账号登录的粗萧疏成。

 1 import getpass
 2 
 3 wall = True
 4 usr = 'root'
 5 paswd = 0000
 6 block_list = open("test.txt", 'a')
 7 print("Please sign in!")
 8 
 9 for i in range(3, 0, -1):
10     print('{time} time(s) left'.format(time=i))
11     usr_in = input("username: ")
12     paswd_in = int(input("password:"))  #
13     # password = getpass.getpass("password:")
14 
15     for line in open('test.txt'):
16         if line == usr:
17             print('Your account is blocked!')
18             wall = False
19             break
20 
21     if wall is False:
22         print('Call you admin!')
23         break
24     elif usr == usr_in and paswd_in == paswd:
25         print('------ Welcome {_name}! ------'.format(_name=usr))
26         break
27     else:
28         if i != 1:
29             print('Invalid password or username!\nPlease try again!')
30         else:
31             print('Your account is blocked!\nCall your admin!')
32             block_list.writelines('{0}'.format(usr))
33 
34 block_list.close()

 

前年10月尾开首python的就学。选取python 3.6。 账号登入的粗糙达成。 1
import getpass 2 3 wall = Tru…

二〇一七年4月首开头python的上学。采取python 3.6。

前年7月首伊始python的学习。采用python 3.6。

Python学习 —— 阶段综联合排演练三,python综合作演出习

Python学习 —— 阶段综合作演出习二,python综合作演出习

  综合此前的类的学习,做以下实例演习:(建议先不用看代码,本人先试着写;代码仅供参谋,有八种落到实处格局)

 

  1. Triangle  & Equilateral

    1). 创立class Triangle
表示三角形,富含三个属性值:angle1、angle2、angle3;

        类方法 check_angles():若四个角相加 == 180,return
True;若不是,return False

图片 1 1 class
Triangle(object): 2 def __init__(self,angle1,angle2,angle3): 3
self.angle1 = angle1 4 self.angle2 = angle2 5 self.angle3 = angle3 6 7
def checkAngles(self): 8 if (self.angle1+self.angle2+self.angle3) == 180
: 9 return True 10 else: 11 return False 12 13 t1 = Triangle(40,50,90)
14 print(t1.angle1,t1.angle2,t1.angle3) 15 print(t1.checkAngles()) 16 t2
= Triangle(40,50,91) 17 print(t2.checkAngles()) Triangle

    2). 创造class Equilateral
承继上例1的Triangle,表示等边三角形,同Triangle差别点在于,其属性值的四个角均为60;而相应的
check_angles() 自然始终重临True

图片 21 class
Equilateral(Triangle): 2 def
__init__(self,angle1=60,angle2=60,angle3=60): 3 self.angle1 = angle1
4 self.angle2 = angle2 5 self.angle3 = angle3 6 7 t3 = Equilateral() 8
print(t3.angle1,t3.angle2,t3.angle3) 9 print(t3.checkAngles()) Equilateral 1

   如上演示代码可知足条件,但越来越好的做法是调用父类构造函数,重写check_angles()
使其始终重回 True,参见代码如下

图片 3 1 class
Equilateral(Triangle): 2 def
__init__(self,angle1=60,angle2=60,angle3=60): 3
Triangle.__init__(self,angle1,angle2,angle3) 4 5 def
checkAngles(self): 6 return True 7 8 t3 = Equilateral() 9
print(t3.angle1,t3.angle2,t3.angle3) 10 print(t3.checkAngles()) Equilateral 2

 

  2.  Car & ElectricCar

    1). 创制class Car 成员变量condition =
“new”,包蕴八个结构属性:model,color,mpg;

        类方法 displayCar() print 拼接的字符串 This is a {color}
{model} car with {mpg} MPG.  如 “This is a blue Xmodel car with 40
MPG.”
        类方法 driveCar() 改形成员变量condition = “used”

图片 4 1 class
Car(object): 2 condition = “new” 3 def
__init__(self,model,color,mpg): 4 self.model = model 5 self.color =
color 6 self.mpg = mpg 7 8 def displayCar(self): 9 print (“This is a
{s.color} {s.model} car with {s.mpg} MPG.”.format(s=self)) 10 11 def
driveCar(self): 12 self.condition = “used” 13 14 car1 = Car(“DeLorean”,
“silver”, 88) 15 car1.displayCar() 16 print(Car.condition) 17
print(car1.condition) 18 car1.driveCar() 19 print(car1.condition) Car

     2). 创立class ElectricCar 承袭 Car,新添一属性别变化量
battery_type;重写driveCar()函数,改变 condition = “like new”

图片 5 1 class
ElectricCar(Car): 2 def
__init__(self,model,color,mpg,battery_type): 3
Car.__init__(self,model,color,mpg) 4 self.battery_type =
battery_type 5 6 def driveCar(self): 7 self.condition = “like new” 8 9
car2 = ElectricCar(“dd”,”Red”,88,”molten salt”) 10
print(car2.battery_type,car2.condition) 11 car2.displayCar() #
继承Car方法 12 car2.driveCar() # 调用重写后的法子 13
print(car2.condition) ElectricCar

 

  3. Point3D

    创立class
Point3D,表示三维坐标上的二个点,富含八个属性别变化量:x,y,z
      类 __repr__ 方法展现为 (x,y,z)
      类方法 distance() 重回改点距原点(0,0,0) 的相距

Python 类方法 __repr__  重写 print class_name 时的显示,参考代码即可理解

图片 6 1
import math 2 class Point3D(object): 3 def __init__(self,x,y,z): 4
self.x = x 5 self.y = y 6 self.z = z 7 def __repr__(self): 8 return
(“({s.x},{s.y},{s.z})”.format(s=self)) 9 10 def distance(self): 11 d =
math.sqrt(self.x**2+self.y**2+self.z**2) 12 return d 13 14 point1
= Point3D(3,4,0) 15 print(point1) 16 print(point1.distance()) Point3D

 

  4. Employee & PartTimeEmployee

    1). 创造class Employee,包蕴成员变量hour_wage
=20,包罗属性别变化量:name;
        类方法 calculateWage() 总计当天工钱,传参hours,return
hours*hour_wage

图片 7 1 class
Employee(object): 2 hour_wage = 20 3 def __init__(self,name): 4
self.name = name 5 6 def calculateWage(self,hours): 7 return
self.hour_wage*hours 8 9 Peter = Employee(“Peter”) 10
print(Peter.calculateWage(5)) Employee

    2). 创设class PartTimeEmployee 承袭Employee,成员变量hour_wage=18, parttime_wage=15 属性变量同Employee
        类方法calculateWage()重写,若是hour>=8,则 return
hour_wage*hours;若是hour<8,return parttime_wage*hours

图片 8 1 class
PartTimeEmployee(Employee): 2 hour_wage=18 3 parttime_wage=15 4 5 def
calculateWage(self,hours): 6 if hours>=8: 7 return
self.hour_wage*hours 8 else: 9 return self.parttime_wage*hours 10 11
May = PartTimeEmployee(“May”) 12 print(May.calculateWage(5)) 13
print(May.calculateWage(8)) PartTimeEmployee

 

—— 阶段综合作演出习二,python综联合排练习综合在此以前的类的上学,做以下实例练习:
(建议先不用看代码,本身先试着写;代码仅供参…

账号登录的粗糙完结。

账号登入的粗疏实现。

Python学习 —— 阶段综合作演出习三

  综合以前的类的学习,做以下实例演习:包涵文件夹及文件的操作(提议先不要看代码,自个儿先试着写;代码仅供参照他事他说加以考察,有各类兑现格局)

 

  1. 索引文件遍历(二层目录结构)

    1).  使用以前学习示例的文件夹模拟;print
出对应目录的目录结构,需缩进;a. 第一行print指标目录 
b.具体的二层目录结构(一层文件夹后加\),文件Gavin件名后缀

    2).  不仅print出结果,将上述print的内容保留至当前专业目录下的
dir_demo.txt 文件中,

      图片 9  可下载后解压至D盘:

        示例的须求结果如下:

        图片 10   
图片 11

图片 12 1 #
!/usr/bin/config python 2 # -*- coding:utf-8 -*- 3 4 import os 5 def
listdir(d,f): 6 d_list = os.listdir(d) # 列出目录下的富有文件和目录 7
print (d) 8 f.write(d + ‘\n’) 9 10 for i in d_list: 11 filepath =
os.path.join(d,i) 12 if os.path.isdir(filepath): # 假使filepath
是目录,则再列出该目录下的装有文件 13 print (‘\t’ + i + ‘\\’) 14
f.write(‘\t’ + i + ‘\\’+’\n’) 15 for li in os.listdir(filepath): 16
print (‘\t\t’+li) 17 f.write(‘\t\t’+li+’\n’) 18 elif os.path: #
假使filepath是文本,直接列出文件名 19 print (‘\t’+i) 20
f.write(‘\t’+i+’\n’) 21 22 23 demo_dir = u”D:\Python_shutil” 24 25
with open(‘dir_demo.txt’,’w’) as f: 26 listdir(demo_dir,f) listdir

  示例使用 "\t" 表示缩进;注意 file.write() 函数不会自动换行,print会打印换行。

 

  2. 在 D:\\demo2 文件夹下,创立5个txt文件,名称及txt的原委分别为
test1、test2 … test5

    1).  判断 D:\\demo2
文件夹是或不是留存,若存在,清空该文件夹里全数文件
(请必需确认保障无你要求的公文);若不真实,创造该文件夹

    2). 
创造txt文件,示例的渴求结果如下:图片 13

图片 14 1
import os,shutil,time 2 3 os.chdir(“d:\\”) 4 dir_name = u”demo2″ 5
dir_abs = os.path.join(os.getcwd(),dir_name) 6 if
os.path.exists(dir_abs): 7 shutil.rmtree(dir_abs) 8 time.sleep(1) #
删除操作之后最佳加个等待时间,不然后续 mkdir 操作或者报错 9
os.mkdir(dir_name) 10 os.chdir(dir_abs) 11 12 for i in range(1,6): 13
txt_name = “test{0}.txt”.format(i) 14 with open(txt_name,”w”) as f: 15
f.write(“test{0}”.format(i)) folder&txt

 

 

 

 

 

 

 

 

 

 

 

—— 阶段综联合排演练三,python综合作演出习
Python学习 阶段综合作演出习三 综合此前的类的求学,做以下实例演习: 包罗文件夹及文件的操…

 1 import getpass
 2 
 3 wall = True
 4 usr = 'root'
 5 paswd = 0000
 6 block_list = open("test.txt", 'a')
 7 print("Please sign in!")
 8 
 9 for i in range(3, 0, -1):
10     print('{time} time(s) left'.format(time=i))
11     usr_in = input("username: ")
12     paswd_in = int(input("password:"))  #
13     # password = getpass.getpass("password:")
14 
15     for line in open('test.txt'):
16         if line == usr:
17             print('Your account is blocked!')
18             wall = False
19             break
20 
21     if wall is False:
22         print('Call you admin!')
23         break
24     elif usr == usr_in and paswd_in == paswd:
25         print('------ Welcome {_name}! ------'.format(_name=usr))
26         break
27     else:
28         if i != 1:
29             print('Invalid password or username!\nPlease try again!')
30         else:
31             print('Your account is blocked!\nCall your admin!')
32             block_list.writelines('{0}'.format(usr))
33 
34 block_list.close()
 1 import getpass
 2 
 3 wall = True
 4 usr = 'root'
 5 paswd = 0000
 6 block_list = open("test.txt", 'a')
 7 print("Please sign in!")
 8 
 9 for i in range(3, 0, -1):
10     print('{time} time(s) left'.format(time=i))
11     usr_in = input("username: ")
12     paswd_in = int(input("password:"))  #
13     # password = getpass.getpass("password:")
14 
15     for line in open('test.txt'):
16         if line == usr:
17             print('Your account is blocked!')
18             wall = False
19             break
20 
21     if wall is False:
22         print('Call you admin!')
23         break
24     elif usr == usr_in and paswd_in == paswd:
25         print('------ Welcome {_name}! ------'.format(_name=usr))
26         break
27     else:
28         if i != 1:
29             print('Invalid password or username!\nPlease try again!')
30         else:
31             print('Your account is blocked!\nCall your admin!')
32             block_list.writelines('{0}'.format(usr))
33 
34 block_list.close()

 

 

相关文章