1.练习一:用户登陆验证
name = input('请输入姓名') password = input('请输入密码') if name == '张三' and password =='123': print('输入正确。。。。') else: print('密码错误')
2.练习二:分段函数
x = eval(input('请输入一个十进制数:')) if x>85 : print("优秀") elif x>60: print('合格') else: print('不合格')
3.打印0-10:
x=0 while x<=10: print(x) x=x+1
4.每秒输入一个数字
import time n=1 while True : print('count',n) time.sleep(1) n+=1
5.
#练习,要求如下: 1 循环验证用户输入的用户名与密码 2 认证通过后,运行用户重复执行命令 3 当用户输入命令为quit时,则退出整个程序
while True: name = input("请输入用户名:") if name == 'quit': break pw = input("请输入密码:") if name == 'abc' and pw == '123': while True: cmd = input('>>: ') if not cmd: continue if cmd == 'quit': break print('run <%s>' % cmd) else : continue break
6。简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型
c为编译型, python为解释型
#编译型语言执行速度快,不依赖语言环境运行,跨平台差 #解释型跨平台好,一份代码,到处使用,缺点是执行速度慢,依赖解释器运行
#1. 编译型(需要编译器,相当于用谷歌翻译):如C,执行速度快,调试麻烦 #2. 解释型(需要解释器,相当于同声传译):如python,执行速度慢,调试方便
7.执行 Python 脚本的两种方式是什么
(1)交互式
(2)python的idel 如pychaim
8.Pyhton 单行注释和多行注释分别用什么?
# 单行
''' duo
hang'''
9.布尔值分别有什么?
True 和 False(区分 大小写)
10.声明变量注意事项有那些?
#1. 变量名只能是 字母、数字或下划线的任意组合 #2. 变量名的第一个字符不能是数字 #3. 关键字不能声明为变量名['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del',
'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
#驼峰体 AgeOfOldboy = 56 NumberOfStudents = 80 #下划线(推荐使用) age_of_oldboy = 56 number_of_students = 80
11如何查看变量在内存中的地址?
id(变量名)
12.实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
count=1 while count<=3 : name = input('请输入姓名') password = input('请输入密码') if (name == '张三' or name =='alex') and password =='123': print('输入正确。。。。') break else: print('密码错误') count +=1
13.现有如下两个变量,请简述 n1 和 n2 是什么关系?
n1 = 123456 n2 = n1
小数据池问题。
类型一致,地址相同
14.
作业:编写登陆接口
基础需求:
- 让用户输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后退出程序
dict={
'aa':{'password':123,'count':0},
'bb':{'password':123,'count':0},
'cc':{'password':123,'count':0},
}
while True:
name= (input("enter name:"))
name=name.strip()
if name not in dict:
continue
if dict[name]['count']>2:
print('尝试次数过多,锁定')
break
pw = eval( input('enter pw:'))
if pw == dict[name]['password']:
print('ok,,,')
break
else :
dict[name]['count']+=1
15.(未完成)
升级需求:
- 可以支持多个用户登录 (提示,通过列表存多个账户信息)
- 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
#db.txt内容:kermit1|kermit2| dic={ 'kermit1':{'password':'123','count':0}, 'kermit2':{'password':'123','count':0}, 'kermit3':{'password':'123','count':0}, } count=0 while True: name=input('u>>: ') if name not in dic: print('用户不存在') continue with open('db.txt','r') as f: lock_users=f.read().split('|') if name in lock_users: print('用户%s已经被锁定' %name) break if dic[name]['count'] > 2: print('尝试次数过多,锁定') with open('db.txt','a') as f: f.write('%s|' %name) break password=input('p>>: ') if password == dic[name]: print('登录成功') break else: print('用户名或密码错误') dic[name]['count']+=1