本节内容:Python的一些简单编程练习,适合初学者。
1.实现一个登陆的功能;
让用户输入用户名,密码,判断用户名密码是否正确,正确容许登陆,不正确不登录。
num=3
username="zhangsunaxiang"
passwd="123456"
flag=True
while num and flag:
login_name = input("请输入用户名:")
login_passwd = input("请输入密码:")
if login_name==username and login_passwd==passwd:
flag=False
print("登录成功")
else:
print("用户名或密码错误!")
num -= 1
if num == 0:
print("登录次数已用完,请100秒后再试")
2.需要实现的功能:
(1)登录,注册(用户名,密码)
(2)用户输入的信息有登录,没有注册并退出
(3)一旦输入错误,提示并重复输入
(4)重复输入的次数不能超过三次。
user_info = {"zhangsan":"123"}
a=0
while a<3:
username = input("username:>>")
password = input("password:>>")
if not username in user_info:
user_info[username] = password
break
elif password == user_info[username]:
print("登录成功!!!")
break
else:
print("密码错误,请重新输入...")
a+=1
else:
print("重复次数太多...")
3.三级目录
level = {
"第一章":{
"1.1 linux编程":{
"用户":["root","user"],
"内核":["3.10.1","uname -a"]
},
"1.2 linux操作":{
"用户":["root","user"],
"内核":["3.10.1","uname -a"]
},
"1.3 linux操作": {
"用户": ["root", "user"],
"内核": ["3.10.1", "uname -a"]
}
},
"第二章":{
"2.1 java编程":{
"语法":["xxx"],
"应用":["none"]
}
},
"第三章":{
"3.1 php编程":{
"框架":["脚本"],
}
}
}
print(level)
while True:
for c1 in level:
print(c1)
chose1 = input(">>>:")
if chose1 in level:
d2 = level[chose1]
while True:
for c3 in d2:
print(c3)
chose2 = input(">>>:")
if chose2 in d2:
d3 = level[chose1][chose2]
while True:
for c4 in d3:
print(c4)
chose3 = input(">>>:")
if chose3 in d3:
d4 = level[chose1][chose2][chose3]
while True:
for c5 in d4:
print(c5)
chose4 = input(">>>:")
if chose4 =="q":
exit()
elif chose4 == "b":
break
else:
print("请正确输入!")
elif chose3 == "q":
exit()
elif chose3 == "b":
break
else:
print("请正确输入!")
elif chose2 == "q":
exit()
elif chose2 == "b":
break
else:
print("请正确输入!")
elif chose1 == "q":
exit()
elif chose1 == "b":
break
else:
print("请正确输入!")