1. 登录作业:

  写一个登录程序,登录成功之后,提示XXX欢迎登录,登录失败次数是3次,要校验一下输入为空的情况

 for i in range(3):
username=input('username:').strip()
passwd=input('passwd:').strip()
if username and passwd:
if username=='weixiaocui' and passwd=='':
print('%s欢迎登录'%username)
break
else:
print('账号/密码错误')
else:
print('账号/密码不能为空')
else:
print('错误失败次数太多!')

2.登录,注册,账号密码 存在文件里面,登录用注册时候的账号和密码

 f=open('user.txt','a+',encoding='utf-8')
f.seek(0)
all_users={}
for line in f:
if line:
line=line.strip()
line_list=line.split(',')
all_users[line_list[0]]=line_list[1]
while True:
username=input('用户名:').strip()
passwd=input('密码:').strip()
cpasswd=input('确认密码:').strip()
if username and passwd and cpasswd:
if username in all_users:
print('用户名已经存在,请重新输入!')
else:
if passwd==cpasswd:
all_users[username]=passwd
f.write(username+','+passwd+'\n')
f.flush()#立即写到文件里面
print('注册成功!')
break
else:
print('两次输入密码不一致,请重新输入!')
else:
print('用户名/密码/确认密码不能为空!')
f.close()
 #登录作业代码
f=open('user.txt',encoding='utf-8')
all_users={}#存放所有用户
for line in f:
if line:
line=line.strip()
line_list=line.split(',')
all_users[line_list[0]]=line_list[1]
while True:
username=input('用户名:').strip()
passwd=input('密码:').strip()
if username and passwd:
if username in all_user:
if passwd == all_users.get(username):
print('欢迎光临 %s'%username)
break
else:
print('密码输入错误!')
else:
print('用户不存在!')
else:
print('用户名/密码不能为空!')
f.close()

3.生成8位密码,输入一个生成的条数,然后生成包含大写,小写字母,数字的密码,写到文件里面

 import random,string
count=int(input('请输入你要生成密码的条数:'))
all_passwds=[]
while True:
lower=random.sample(string.ascii_lowercase,1)#随机取一个小写字母
upper=random.sample(string.ascii_uppercase,1)#随机取一个大写字母
num=random.sample(string.digits,1)#随机取一个数字
other=random.sample(string.ascii_letters+string.digits,5)
res=lower+upper+num+other#把这4个list合并到一起
random.shuffle(res)#打乱顺序
new_res=''.join(res)+'\n'#因为res是list,所以转成字符串
if new_res not in all_passwds:#这里是为了判断是否重复
all_passwds.append(new_res)
if len(all_passwds)==count:#判断循环什么时候结束
break
with open('passwds.txt','w') as fw:
fw.writelines(all_passwds)

4.判断字典里面不一样的key,字典可能有多层嵌套

  循环字典,从一个字典里面取到key,然后再从第二个字典里面取k,判断value是否一样

 ok_req={
"version": "9.0.0",
"is_test": True,
"store": "",
"urs": "",
"device": {
"os": "android",
"imei": "",
"device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
"mac": "02:00:00:00:00:00",
"galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
"udid": "a34b1f67dd5797df93fdd8b072f1fb8110fd0db6",
"network_status": "wifi"
},
"adunit": {
"category": "VIDEO",
"location": "",
"app": "7A16FBB6",
"blacklist": ""
},
"ext_param":{
"is_start" : 0,
"vId":"VW0BRMTEV"
}
}
not_ok={
"version": "9.0.0",
"is_test": True,
"urs": "",
"store": "",
"device": {
"os": "android",
"imei": "",
"device_id": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
"mac": "02:00:00:00:00:00",
"galaxy_tag": "CQliMWEyYTEzNTYyYzk5MzJmCTJlNmY3Zjkx",
"udid": "a34b1f67dd5797da93fdd8b072f1fb8110fd0db6",
"network_status": "wifi"
},
"adunit": {
"category": "VIDEO",
"location": "",
"app": "7A16FBB6",
"blacklist": ""
},"ext_param": {
"is_start": 0,
"vid": "VW0BRMTEV"
}
}
def compare(d1,d2):
for k in d1:
v1=d1.get(k)
v2=d2.get(k)
if type(v1)==dict:
compare(v1,v2)
else:
if v1!=v2:
print('不一样的k是【%s】,v1是【%s】 v2是【%s】'%(k,v1,v2))
#对称差集,两个集合里面都没有的元素,在a里有,在b里没有,在b里有,在a里没有的
res=set(d1.keys()).symmetric_difference(set(d2.keys()))
if res:
print('不一样的k',res)
compare(ok_req,not_ok)

5.商品管理的程序

  1.添加商品

  2.查看商品

  3.删除商品

  4.商品都存在文件里面

  5.存商品的样式:{'car':{'color':'red','count':5,'price':100},'car2':{'color':'red','count':5,'price':100}}

 PRODUCTS_FILE='products'
def op_file(filename,content=None):
f=open(filename,'a+',encoding='utf-8')
f.seek(0)
if content!=None:
f.truncate()
f.write(str(content))
f.flush()
else:
res=f.read()
if res:
products=eval(res)
else:
products={}
return products
f.close()
def check_price(s):
s = str(s)
if s.count('.')==1:
left=s.split('.')[0]
right=s.split('.')[1]
if left.isdigit() and right.isdigit():
return True
elif s.isdigit() and int(s)>0:
return True
return False
def check_count(count):
if count.isdigit():
if int(count)>0:
return True
return False
def add_product():
while True:
name=input('name:').strip()
price=input('price:').strip()
count=input('count:').strip()
color=input('color:').strip()
if name and price and count and color:
products=op_file(PRODUCTS_FILE)
if name in products:
print('商品已经存在')
elif not check_price(price):
print('价格不合法')
elif not check_count(count):
print('商品数量不合法')
else:
product={'color':color,'price':'%.2f'%float(price),'count':count}
products[name]=product
op_file(PRODUCTS_FILE,products)
print('商品添加成功')
break
else:
print('必填参数未填')
def view_products():
products=op_file(PRODUCTS_FILE)
if products:
for k in products:
print('商品名称【%s】,商品信息【%s】'%(k,products.get(k)))
else:
print('当前没有商品')
def del_products():
while True:
name=input('name:').strip()
if name:
products=op_file(PRODUCTS_FILE)
if products:
if name in products:
products.pop(name)
op_file(PRODUCTS_FILE,products)
print('删除成功!')
break
else:
print('你输入的商品名称不存在,请重新输入!')
else:
print('当前没有商品')
else:
print('必填参数未填')
def start():
choice=input('请输入你的选择:1、添加商品2、查看商品3、删除商品,q、退出:').strip()
if choice=='':
add_product()
elif choice=='':
view_products()
elif choice=='':
del_products()
elif choice=='q':
quit('谢谢光临')
else:
print('输入错误!')
return start()
start()

6.写清理日志的一个脚本,要求转入一个路径,只保留3天以内的日志,剩下的全部删掉。

 import os,datetime
def clean_log(path):
if os.path.exists(path) and os.path.isdir(path):
today = datetime.date.today() #2017-01-02
yesterday = datetime.date.today()+ datetime.timedelta(-1)
before_yesterday = datetime.date.today()+ datetime.timedelta(-2)
file_name_list = [today,yesterday,before_yesterday]
for file in os.listdir(path):
file_name_sp = file.split('.')
if len(file_name_sp)>2:
file_date = file_name_sp[1] #取文件名里面的日期
if file_date not in file_name_list:
abs_path = os.path.join(path,file)
print('删除的文件是%s,'%abs_path)
os.remove(abs_path)
else:
print('没有删除的文件是%s'%file)
else:
print('路径不存在/不是目录')
clean_log(r'')

python作业习题集锦的更多相关文章

  1. Python作业第一课

    零基础开始学习,最近周边的同学们都在学习,我也来试试,嘿嘿,都写下来,下次不记得了还能来看看~~ Python作业第一课1)登陆,三次输入锁定,下次不允许登陆2)设计一个三级菜单,菜单内容可自行定义, ...

  2. Python作业-选课系统

    目录 Python作业-选课系统 days6作业-选课系统: 1. 程序说明 2. 思路和程序限制 3. 选课系统程序目录结构 4. 测试帐户说明 5. 程序测试过程 title: Python作业- ...

  3. python作业ATM(第五周)

    作业需求: 额度 15000或自定义. 实现购物商城,买东西加入 购物车,调用信用卡接口结账. 可以提现,手续费5%. 支持多账户登录. 支持账户间转账. 记录每月日常消费流水. 提供还款接口. AT ...

  4. (转)Python作业day2购物车

    Python作业day2购物车 原文:https://www.cnblogs.com/spykids/p/5163108.html 流程图: 实现情况: 可自主注册, 登陆系统可购物,充值(暂未实现) ...

  5. Python开发面试集锦

    我正在编写一套python面试开发集锦,可以帮忙star一下,谢谢! 地址:GitHub专栏

  6. Python作业之三次登陆锁定用户

    作业之三次登陆锁定用户 作业要求如下: 1. 输入用户名和密码 2. 认证成功提示欢迎信息 3. 认证失败三次锁定用户 具体代码如下: 方法1: import os#导入os模块 if os.path ...

  7. python作业高级FTP

    转载自:https://www.cnblogs.com/sean-yao/p/7882638.html 作业需求: 1. 用户加密认证 2. 多用户同时登陆 3. 每个用户有自己的家目录且只能访问自己 ...

  8. Python作业

    1使用while 循环输入1,2,3,4,5,6,,8,9,10 count = 0 while count<10: count+=1 if count ==7: continue print( ...

  9. python作业简单FTP(第七周)

    作业需求: 1. 用户登陆 2. 上传/下载文件 3. 不同用户家目录不同 4. 查看当前目录下文件 5. 充分使用面向对象知识 思路分析: 1.用户登陆保存文件对比用户名密码. 2.上传用json序 ...

随机推荐

  1. mysql 之 frm+ibd文件还原data

      此方法只适合innodb_file_per_table          = 1 当误删除ibdata 该怎么办? 如下步骤即可恢复: 1.准备工作 1)准备一台纯洁的mysql环境[从启动到现在 ...

  2. Bing Advanced Search Tricks You Should Know

    Bing is one of the world's most popular search engines that has gained many fans with its ease of us ...

  3. 爬虫(五)—— 解析库(二)beautiful soup解析库

    目录 解析库--beautiful soup 一.BeautifulSoup简介 二.安装模块 三.Beautiful Soup的基本使用 四.Beautiful Soup查找元素 1.查找文本.属性 ...

  4. 自定义SAP搜索帮助记录-代码实现

    一般来说,标准的字段都可以用SE11来创建搜索帮助,但是有时候这里的满足不了需求或者,相同的数据元素需要用不同的搜索帮助类型,就需要用别的方式实现 1.用函数:F4IF_INT_TABLE_VALUE ...

  5. HashMap -双列集合的遍历与常用的方法

    package cn.learn.Map; /* java.util.Hashtable<k,y> implements Map<k,v> 早期双列集合,jdk1.0开始 同步 ...

  6. linux下shell显示git当前分支

    function git-branch-name { git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3 } functio ...

  7. [Linux] 023 RPM 包校验与文件提取

    1. RPM 包校验 $ rpm -V 已安装的包名 选项 释义 -V (verify) 校验指定 RPM 包中的文件 (1) 验证内容中的 8 个信息的具体内容如下 信息名称 释义 S 文件大小是否 ...

  8. MyEclipse停止自带插件的启动

    MyEclipse启动时因为自身带有很多的插件,所以在启动时运行的速度特别慢,所以可以选择一下启动时的插件,将不使用的插件选择在MyEclipse启动时不起动. 步骤如下: windows->p ...

  9. mysql 5.7 创建函数报错,This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creat

    今天用命令创建函数, 报错 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration ...

  10. POJ 3764 The xor-longest Path (01字典树)

    <题目链接> 题目大意: 给定一颗$n$个节点$(n\leq10^5)$,有边权的树,其边权$(0\leq w < 2^{31})$.让你求出这棵树上任意两个节点之间的异或最大值. ...