编程语言主要从以下几个角度进行分类,编译型和解释型、静态语言和动态语言、强类型定义语言和弱类型定义语言。
解释型语言更容易移植
综上所述:python是一门动态解释性的强类型定义语言
python2默认用ASCII进行编码,python3默认用utf-8进行编码。
format格式化输出
name = input('name:')
age = input('age:')
job = input('job:')
salary = input('salary:')
#第一种format格式化输出方式
info = '''
---------- info of {_name}---------
Name:{_name}
age:{_age}
job:{_job}
salary:{_salary}
'''.format(_name=name,_age=age,_job=job,_salary=salary)
#第二种format格式化输出方式
info2 = '''
---------- info of {0}---------
Name:{0}
age:{1}
job:{2}
salary:{3}
'''.format(name,age,job,salary)
print(info2)
密文输入
python中以缩进来代表代码块,这样不仅可以节省代码量,而且结构清晰明了
while循环
age = 56
count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age:
print("答对了!")
break
elif guess_age > age:
print("大了!")
else:
print("小了")
count += 1
#while正常走完流程就执行
else:
print("你已经试了太多次了")
# 编写登录接口
# 1.输入用户名密码
# 2.认证成功后显示欢迎信息
# 3.输错三次后锁定
import ast
def login():
count = 0
with open("用户信息文件",'r',encoding='utf-8') as f:
infos = f.readlines()
# print(infos)
while True:
username = input("请输入用户名:")
password = input("请输入密码:")
for info in infos:
if username in info:
info_dic = ast.literal_eval(info) #字符串转字典
if count >= 1:
lock_info = info.replace("'status': '0'", "'status': '1'")
# print('lock_info',lock_info)
# print(infos)
with open("用户信息文件", 'w', encoding='utf-8') as fp:
for new_info in infos:
# print(new_info)
if username in new_info:
fp.write(lock_info)
continue
fp.write(new_info)
print("%s用户已被锁定" % info_dic['username'])
return
elif info_dic['status'] == '1':
print("%s用户已被锁定"%info_dic['username'])
return
elif username == info_dic['username'] and password == info_dic["password"]:
print("登录成功")
return
else:
print("用户名或密码错误,请重新登录!!!")
count += 1
# print(count)
break
else:
print("用户不存在")
return
if __name__ == '__main__':
login()