python第三周测试_python第三周小测
1.讀取一個(gè)文件,顯示除了井號(hào)(#)開(kāi)頭的行意外的所有行
# -*- coding: utf-8 -*-
"""
Created on Tue May 28 09:37:08 2019
@author: Omega_Sendoh
"""
#打開(kāi)文件
f = open("install-sh","r")
#讀取文件的所有行,以列表形式存儲(chǔ),每行為列表中的一個(gè)字符串元素
res = f.readlines()
#循環(huán)整個(gè)列表,去除以空格開(kāi)頭的行的空格,然后去除以#號(hào)開(kāi)頭的行的#號(hào)
for i in res:
if i[0] == "#":
continue
else:
print(i)
2.無(wú)重復(fù)字符的最長(zhǎng)子串
# -*- coding: utf-8 -*-
"""
Created on Tue May 28 10:57:55 2019
@author: Omega_Sendoh
"""
"""
定義一個(gè)空的字符串,從起始位置開(kāi)始搜索輸入的字符串,如果字符沒(méi)有出現(xiàn)在wind中,則把字符加入wind
如果字符出現(xiàn)在wind中,則找到字符串中出現(xiàn)該相同字符的位置,刪除該重復(fù)字符之前的所有字符
并重新加入該字符
"""
def LongestString(s):
wind = ''
l=0
for i in s:
if i not in wind:
wind +=i
l=max(l,len(wind))
else:
wind = wind[wind.index(i)+1:] + i
return l
s=input('enter string:')
print(LongestString(s))
3.制作一個(gè)密碼簿,其可以存儲(chǔ)一個(gè)網(wǎng)址,和一個(gè)密碼,請(qǐng)編寫(xiě)程序完成這個(gè)密碼簿的增刪改查功能,并且實(shí)現(xiàn)文件存儲(chǔ)功能。
import json
def add_info():
#打開(kāi)存儲(chǔ)文件,判斷文件中是否有內(nèi)容
with open('usr.json','a+') as f:
info = f.read()
#如果沒(méi)有內(nèi)容,創(chuàng)建一個(gè)字典,以字典的方式存儲(chǔ)網(wǎng)址與密碼
if not info:
with open('usr.json','a+') as f:
full_info = {}
net_add = input('enter your URL:')
passwd = input('enter your password:')
full_info[net_add] = passwd
print('add success')
json.dump(full_info,f)
#若文件中有內(nèi)容,則把文件中的內(nèi)容轉(zhuǎn)換為python的字典類(lèi)型
else:
with open('usr.json','r') as f :
full_info = json.load(f)
#print((full_info))
net_add = input('enter your URL:')
passwd = input('enter your password:')
full_info.setdefault(net_add,passwd)
print('add success')
#給字典中添加對(duì)象,再次寫(xiě)入文件中(即添加新的信息后重新更新文件的內(nèi)容)
with open('usr.json','w') as f :
json.dump(full_info,f)
def del_info():
with open('usr.json','r') as f:
full_info = json.load(f)
#輸入想要?jiǎng)h除的網(wǎng)址
net_add = input('enter your delete URL:')
#如果該網(wǎng)址存在,則刪除網(wǎng)址與密碼,把更改后的數(shù)據(jù)存到文件中
if net_add in full_info:
del full_info[net_add]
print('delete success')
with open('usr.json','w') as f:
json.dump(full_info,f)
#若該網(wǎng)址不存在,提示網(wǎng)址不存在
else:
print('This URL not exist')
def change_info():
with open('usr.json','r') as f:
full_info = json.load(f)
#輸入要更改的網(wǎng)址與密碼
net_add = input('enter your change URL:')
passwd = input('enter your new password:')
if net_add in full_info:
full_info[net_add] = passwd
print('change password succcess')
with open('usr.json','w') as f:
json.dump(full_info,f)
else:
print('This URL not exist')
def check_info():
with open('usr.json','r') as f:
full_info = json.load(f)
net_add = input('enter your check URL:')
if net_add in full_info:
print('This URL password is:',full_info[net_add])
else:
print('This URL not exist')
print('you can choose: add/del/change/check/quit')
while 1:
obj = input('enter your choose:')
if obj == 'add':
add_info()
elif obj == 'del':
del_info()
elif obj == 'change':
change_info()
elif obj == 'check':
check_info()
else:
break
運(yùn)行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的python第三周测试_python第三周小测的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 1、如何进行字符串常量中的字符定位_Ja
- 下一篇: 妙用PRN文件,实现文档换机打印