python第三週小測

2021-09-23 23:54:11 字數 3563 閱讀 9197

1.讀取乙個檔案,顯示除了井號(#)開頭的行意外的所有行

# -*- coding: utf-8 -*-

"""created on tue may 28 09:37:08 2019

@author: omega_sendoh

"""#開啟檔案

f = open("install-sh","r")

#讀取檔案的所有行,以列表形式儲存,每行為列表中的乙個字串元素

res = f.readlines()

#迴圈整個列表,去除以空格開頭的行的空格,然後去除以#號開頭的行的#號

for i in res:

if i[0] == "#":

continue

else:

print(i)

2.無重複字元的最長子串

# -*- coding: utf-8 -*-

"""created on tue may 28 10:57:55 2019

@author: omega_sendoh

""""""

定義乙個空的字串,從起始位置開始搜尋輸入的字串,如果字元沒有出現在wind中,則把字元加入wind

如果字元出現在wind中,則找到字串**現該相同字元的位置,刪除該重複字元之前的所有字元

並重新加入該字元

"""

def longeststring(s):

wind = ''

l=0for 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.製作乙個密碼簿,其可以儲存乙個**,和乙個密碼,請編寫程式完成這個密碼簿的增刪改查功能,並且實現檔案儲存功能。

import json

def add_info():

#開啟儲存檔案,判斷檔案中是否有內容

with open('usr.json','a+') as f:

info = f.read()

#如果沒有內容,建立乙個字典,以字典的方式儲存**與密碼

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)

#若檔案中有內容,則把檔案中的內容轉換為python的字典型別

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')

#給字典中新增物件,再次寫入檔案中(即新增新的資訊後重新更新檔案的內容)

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)

#輸入想要刪除的**

net_add = input('enter your delete url:')

#如果該**存在,則刪除**與密碼,把更改後的資料存到檔案中

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)

#若該**不存在,提示**不存在

else:

print('this url not exist')

def change_info():

with open('usr.json','r') as f:

full_info = json.load(f)

#輸入要更改的**與密碼

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

執行結果:

python自學 第三週

coding utf 8 from functools import reduce 處理序列中的每個元素,得到的結果是乙個 列表 迭代器 元素個數與位置和原來的一樣 map print list filter lambda p p age 18,p print list zip p s slice ...

Python 第三週作業

1.完美立方 程式設計題 n int input n範圍內的立方數 list cube 0 用於儲存立方數的列表 for i in range 1,n 1 for a in range 6,n 1 for b in range 2,a 1 if list cube a list cube b lis...

python課程第三週小結 python週報第三週

1.set 集合 1.set 方法解析 1.定義乙個set s1 caesary set1 set s1 print set1 l1 1,2,3,4 set2 set l1 print set2 t1 1,1,1,2,3 set3 set t1 print set3 執行結果如下 總結 set集合有...