玩蛇(Python)笔记之基础Part1

玩蛇笔记之基础Part1

一.国际惯例之Hellow World!!

 

1 # Hello World
2 print("Hello World!! ")
3 print("Hello again!! \n")
4 print("Hello again!!")
Hellow World

 

二.定义变量

1.Python声明或定义变量时无需再变量名前指出变量的类型,由python自行判断.

 1 # define variable
 2 
 3 name = "周易"
 4 age = 23
 5 province = 'Anhui'
 6 company = 'Hehehe'
 7 
 8 newName = name
 9 name = 'hehehe'
10 
11 print(name, newName, age, province, company)
Variable

2.常量名全大写

1 # -*- coding:utf-8 -*-
2 # Author:YEAR
3 
4 # 常量名大写!!!
5 MYSQL_CONNECTION = 'biubiubiu'
Constant

 

三.先弄些基本函数玩一玩

1.input函数从标准输入读

2.int函数将str转换成int类型

3.print函数将向标准输出输出内容 这里注意和py2的区别

 1 # -*- coding:utf-8 -*-
 2 # Author:YEAR
 3 
 4 
 5 name = input("input your name:")
 6 age = int(input("input your age:"))
 7 job = input("input your job:")
 8 
 9 msg = '''
10 Information of yourself:
11 ------------------------
12 Your name:   %s
13 Your age:    %d
14 Your job:    %s
15 ------------------------
16 ''' % (name, age, job)
17 
18 print(msg)
19 
20 # print("user input message:", user_input)
21 # print('user input message: {usermessage}'.format(usermessage=user_input))
input int print

4.导入常见模块os并用其在终端执行命令并拿到结果

 1 # -*- coding:utf-8 -*-
 2 # Author:YEAR
 3 
 4 import getpass
 5 import os
 6 
 7 # username = input("username:")
 8 # password = getpass.getpass("password:")
 9 # print(username, password)
10 # cmdBack = os.system("df -h") #拿到命令执行后的返回code
11 cmdBack = os.popen("df -h").read()
12 # os.mkdir('biubiubiu')
13 print(cmdBack)
os model

 

四.简单示例

1.猜年龄

 1 # -*- coding:utf-8 -*-
 2 # Author:YEAR
 3 
 4 user = 'YEAR'
 5 passwd = 'year93926'
 6 age = 23
 7 
 8 username = input("username:")
 9 password = input("password:")
10 
11 # if user == username:
12 #     print("username is correct...")
13 #     if password == passwd:
14 #         print("Welcome login.....")
15 #     else:
16 #         print("Password is invalid!!")
17 # else:
18 #     print("用户名都没写对 滚粗!!!")
19 if user == username and passwd == password:
20     print("Welcome login..you have 3 chance")
21     counter = 0
22     for i in range(10):
23         if counter < 3:
24             guess_num = int(input("input your guess num:"))
25             if guess_num == age:
26                 print("You are right")
27                 break
28             elif guess_num > age:
29                 print("bigger")
30             else:
31                 print("smaller")
32         else:
33             # print("You have try too many time!!")
34             # break
35             continue_confirm = input("Do you want to continue bescause you are stupid:")
36             if continue_confirm == 'y':
37                 counter = 0
38                 continue
39             else:
40                 break
41         counter += 1
42 
43 else:
44     print("给你个飞天大草!!!")
guess age

  2.登录 连续登录三次锁死

 1 # Author:YEAR
 2 
 3 import os
 4 
 5 user = ['YEAR']
 6 passwd = {'YEAR': 'year93926'}
 7 errorUser = ''
 8 counter = 0
 9 lockUserfile=os.getcwd()+'HW1_lockUser.txt'
10 lockflag=0
11 
12 while True:
13     username = input("Please input your user name:")
14     password = input("Please input your password:")
15     #先判断用户是否被锁
16     if os.path.exists(lockUserfile):
17         with open(lockUserfile, 'r') as f:
18             for line in f.readlines():
19                 if line.strip() == username:
20                     print("This username has been locked!!")
21                     lockflag=1
22                     break
23         if lockflag==1:
24             lockflag=0
25             continue
26 
27     else:
28         pass
29     if username in user:
30         if password == passwd[username]:
31             print("Good you login success!!")
32             break
33         else:
34             if errorUser == username:
35                 counter += 1
36                 if counter == 3:
37                     # 用户名加入锁死账号文件 errorUser和counter清空
38                     with open(lockUserfile,'a') as f:
39                         f.write(username)
40                         f.write("\n")
41                     errorUser = ''
42                     counter = 0
43                     print("Your input username has be locked!!")
44                     continue
45                 else:
46                     print("If you lianxu input error 3 times your username will be locked!!")
47                     continue
48             else:
49                 errorUser = ''
50                 counter = 0
51                 print("If you lianxu input error 3 times your username will be locked!!")
52                 continue
53     else:
54         if errorUser == username:
55             counter +=1
56             if counter==3:
57                 #用户名加入锁死账号文件 errorUser和counter清空
58                 with open(lockUserfile, 'a') as f:
59                     f.write(username)
60                     f.write("\n")
61                 errorUser = ''
62                 counter = 0
63                 print("Your input username has be locked!!")
64                 continue
65             else:
66                 print("If you lianxu input error 3 times your username will be locked!!")
67         else:
68             errorUser=''
69             counter=0
70             print("If you lianxu input error 3 times your username will be locked!!")
login

 

 

 

posted @ 2016-11-13 20:04  YEAR~  阅读(161)  评论(0编辑  收藏  举报