Python第一周_所写代码_作业
登录接口
在目录下创建一个locked_user的文档用于被函数读取
# Author:转载
import os
current_path = os.getcwd() # 获取当面目录
db_username = ['alex', 'xiaoya', 'kunpeng', 'asd'] # 用户名数据库,用户名不能是一样的
db_password = [123, 123, 123, 123] # 密码可以是一样的
locked_file = open(current_path + '/locked_user.txt') # 打开文件
locked_info = locked_file.readlines() # 获取被锁定用户的信息。readlines一次读取所有文件,并返回list.因为我们不止锁定一个用户,所以用这个方法
locked_file.close()
count = 3
while count > 0:
count -= 1
username = input('username:').lower()
if (username + '\n') in locked_info: # 判断用户是否被锁定,加\n是因为下面用了追加模式,每追加一个用户名就重起一行
print('用户名已被锁定')
break
password = int(input('password:'))
if username in db_username and password in db_password: # 用户名和密码都在数据库里才能验证通过
print('welcome')
break
else:
if username in db_username:
if count == 0:
locked_file = open(current_path + '/locked_user.txt', 'a') # 在locked_user.txt里用追加模式储存这个用户名,每追加一个重起一行
locked_file.write(username + '\n')
locked_file.close()
print('登录三次失败,用户名被锁定')
else:
print('用户名或错误,你还有{}次机会'.format(count))
else:
if count == 0:
print('用户名不存在,登陆失败')
else:
print('用户名不存在,你还有{}次机会'.format(count))
附上流程图:(这个是我写的,不保证能走得通)

文字游戏
# Author:转载
date = {
'北京':{
"平昌":{
"沙河":{"腾讯","华为","小米"},
"天同怨":{"百度","链家地产","我爱我家地产"}
},
"朝阳":{
"望京":{"奔驰","宝马","福特","长安"},
"国贸":{"CICC","ATCC","HP"},
"东直门":{"华硕","阿里巴巴","京东"}
}
},
'四川':{
"成都":{
"青羊":{"八宝街","春熙路","阳公桥"},
"武侯":{"武侯祠","外双楠","火车北站"},
"双流":{"蛟龙港","海滨城","双流万达广场"},
"郫县":{"红光镇","富士康","联想"}
},
"绵阳":{
"万顺":{"草上飞"},
"城区":{"ODDL","DBCC"}
},
"西昌":{
"南城":{"长途汽车站","东方元"},
"北城":{"琼海","火车站","工业区"}
}
},
'云南':{
"昆明":{
"城南":{"大学城","双都飞"},
"城北":{"工业区","农业区"}
},
"西双版纳":{
"南城":{"旅游区","度假区","休闲区"},
"西城":{"娱乐区","双阳区","不知道"}
},
"昭通":{
"南城":{"山顶","无极"},
"东城":{"火车站","汽车站"}
}
},
'广东':{
"珠海":{
"北城":{"商业","旅游","工作"},
"西城":{"富家","双极"}
},
"东莞":{
"城西":{"吃","喝","玩","OPP"}
},
"佛山":{
"北城":{"美的","OPPO","VIVO","HUAWEI"},
"东城":{"格力","长虹","小天使"}
}
}
}
exit_flag = False
# while循环 可以多次返回
while True :
# for循环遍历字典中的数据
for i in date :
print(i)
# 用户输入选择进入
choice = input("选择进入>>>>>")
if choice in date :
while not exit_flag :
#输入选择后 用for循环再次遍历字典的下一层 再次让用户选择输入 再进入下一层 一次循环进入下一层
for two in date[choice] :
print("\t",two)
choice2 = input("选择进入2>>>>>")
if choice2 in date[choice] :
while not exit_flag :
for therr in date[choice][choice2]:
print("\t\t",therr)
# 进入字典的第三层
choice3 = input("选择进入3>>>>")
if choice3 in date[choice][choice2]:
for frou in date[choice][choice2][choice3]:
print("\t\t",frou)
# 这里是最后一层 让用户选择输入 输入B 就返回上一级
choice4 = input("这里是最后一层,输入B/b返回")
# 判断用户输入的是什么
if choice4 == "B" or choice4 == "b":
pass
# 如果输入Q就退出 下面的内容都是一样的道理
elif choice4 == "q" or choice4 == "Q":
exit_flag = True
if choice3 == "b" or choice3 == "B":
break
elif choice3 == "q" or choice3 == "Q":
exit_flag = True
if choice2 == "b" or choice2 == "B":
break
elif choice2 == "q" or choice2 == "Q":
exit_flag = True
附上我的流程图:


浙公网安备 33010602011771号