一、python入门
1、python的标准格式
创建python文件以.py结尾
如:vi hello.py
#!/usr/bin/env python
#-*- coding:utf-8 -*- #python3.5以上版本默认可以使用中文。
print "Hello Word"
执行:
python hello.py 即可
或者增加执行权限:chmod 755 hello.py
./hello.py
2、变量
name = "liudong"
name2 = name
然后把name = "liuyansheng"
#name2 不变,原因是一旦修改,重新创建。name2会重新开辟一块新内存空间。
集合就不同了:
name = ['liu','yan','sheng',]
name2 = name
#name2会变,原因是集合和字符串不同他的各个元素不是连续的,name增加一个元素不会开辟一块新的空间,name2就会跟着改变。
3、输入
name = raw_input("请输入用户名:")
#打印输出的内容
print name
输入密码时,如果想隐藏密码,需要利用getpass模块中的getpass方法,即:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#liudong
import getpass
#将用户输入的内容赋值给name变量
pwd = getpass.getpass("请输入密码:")
#打印输入的内容
print name
4、流程控制
(1)用户身份认证:
语法:if...else if...elif...elif...else
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#liudong
import getpass
name = raaw_input("请输入用户名:")
pwd = raw_input("请输入密码:")
if name =="liudong" and pwd =="123456":
print "登陆成功"
else:
print "登陆失败"
View Code
(2)猜年龄:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#liudong
liudong = 24
guess_age = int(input("guess age:"))
if guess_age == liudong:
print ("yes")
elif guess_age > liudong:
print ("smaller....")
else:
print ("bigger....")
View Code
循环猜3次,猜对退出:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#liudong
liudong = 24
count = 0
while True:
if count == 3:
break
guess_age = int(input("guess age:"))
if guess_age == liudong:
print ("yes")
break
elif guess_age > liudong:
print ("smaller....")
else:
print ("bigger....")
count +=1
View Code
最多循环猜3次,猜对自动退出,猜错提示“fuck off”:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#liudong
liudong = 24
count = 0
while count < 3
guess_age = int(input("guess age:"))
if guess_age == liudong:
print ("yes")
break
elif guess_age > liudong:
print ("smaller....")
else:
print ("bigger....")
count +=1
else:
print ("fuck off")
View Code
注:continue 是跳出当前循环,break是跳出整个循环。
《用户登陆》-代码
https://www.processon.com/diagraming/57985ffde4b0bc50fa52a09b 用户登陆流程图
注:新建user_info.txt用户文件、和user_lock_info.txt文件。
完整代码如下:
#!/usr/bin/env/python
import sys,os,getpass
#os.system('clear') #清屏
i=0
while i < 3: #只要用户异常登录不超过3次,可以不断循环
name = input('\33[32m请输入用户名:\33[37m')
lock_file = open('user_lock_info.txt','a+')
#当用户输入用户名后,打开锁定文件 检查此用户是否已经lock掉了
lock_list = lock_file.readlines() #按行的形式打开
for lock_line in lock_list: #循环lock文件
lock_line =lock_line.strip('\n') #去掉换行符
if name == lock_line: #如果lock,直接退出
sys.exit('用户 %s 已经被锁定,退出!' % name)
user_file = open('user_info.txt') #打开账号文件
user_list = user_file.readlines()
for user_line in user_list: #对账号文件进行循环匹配
(user,password) = user_line.strip('\n').split() #分别获取账号、密码
if name == user: #如果用户名正常匹配
a = 0
while a < 3: #只要用户密码异常输入不超过3次就不断循环
passwd = input('\33[32m请输入密码:\33[37m') #输入密码,也可以做隐藏密码
if passwd == password: #密码正确匹配,提示欢迎登录
print('欢迎登陆管理平台,用户 %s !' % name)
sys.exit(0) #正常退出,调用系统退出命令
else :
if a != 2: #a=2时,是最后一次机会,不会提示0次机会了
print('用户 %s 密码错误,请重新输入,还有 %d 次机会' % (name,2 - a))
a += 1 #密码输错后,循环增加1
else:
lock_file.write(name + '\n') #密码输错三次后,将该用户写入lock文件
sys.exit('用户 %s 达到最大登陆次数将被锁定退出' % name)
else:
if i != 2: #i=2时,是最后一次机会,不再提示0次机会了
print ('用户 %s 不存在,请重新输入,还有 %d 次机会' % (name,2 -i))
i += 1 #当用户输错时,循环增加1
else:
print('用户 %s 不存在,退出!') #用户输入3次错误后,异常退出
lock_file.close() #关闭lock文件
user_file.close() #关闭账号、密码文件
《用户登陆》-代码
https://www.processon.com/diagraming/57986e66e4b0636f3aa4d175 三级菜单流程图
完整代码如下:
#!/usr/bin/env python
#-*- encoding:utf8 -*-
chinamap = {
"山东省":{
"济南":["市中区1","历下区2","天桥区","槐荫区","历城区","长清区","章丘市","平阴县","济阳县","商河县","其他"],
"青岛":["市南区","市北区","城阳区","四方区","李沧区","黄岛区","崂山区","胶南市","胶州市","平度市","莱西市","即墨市","其他"]
},
"北京市":{
"北京":["东城区","西城区","崇文区","宣武区","朝阳区","海淀区","丰台区","石景山区","房山区","通州区","顺义区","昌平区","大兴区","怀柔区","平谷区","门头沟区","密云县","延庆县","其他"],
},
"广东省":{
"广州":["越秀区","荔湾区","海珠区","天河区","白云区","黄埔区","番禺区","花都区","南沙区","萝岗区","增城市","从化市","其他"],
"深圳":["福田区","罗湖区","南山区","宝安区","龙岗区","盐田区","其他"]
}
}
flag = True
try_sheng = 0
try_city = 0
try_area = 0
while flag:
if try_sheng < 3 :
print ('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
for sheng in chinamap.keys(): #获取变量的键
print (sheng)
print ('============================================================')
shengfen_input = input('请输入要查询的省份:')
city_flag = True
while city_flag:
if try_city < 3 :
if shengfen_input.strip() in chinamap:
#执行下面的操作
print ('下面请输入城市信息')
print ('============================================================')
for city in chinamap[shengfen_input].keys():
print (city)
print ('============================================================')
city_input = input('请输入要查询的城市名称:')
area_flag = True
while area_flag:
if try_area < 3 :
if city_input.strip() in chinamap[shengfen_input]:
print ('下面请输入城区信息')
print ('============================================================')
for city_area in chinamap[shengfen_input][city_input]:
print (city_area)
print ('============================================================')
area_input = input('请输入要查询的区域信息:')
if area_input.strip() in chinamap[shengfen_input][city_input]:
print ('当前查询的是:',shengfen_input,city_input,area_input)
flag = False
city_flag = False
break
else:
print ('没有要查询的区域,请重新输入')
try_area += 1
else:
print ("没有要查询的城市,请重新输入")
try_city +=1
area_flag = False
else:
print ('三次操作错误,退出程序')
flag = False
city_flag = False
area_flag = False
else:
print ("没有要查询的省份,请重新输入")
try_sheng += 1
city_flag = False
else:
print ('三次操作错误,退出程序')
flag = False
city_flag = False
else :
print ('三次操作错误,退出程序')
flag = False