14 作業3月18號

2022-09-06 13:06:24 字數 2034 閱讀 5369

1、寫函式,,使用者傳入修改的檔名,與要修改的內容,執行函式,完成批了修改操作

def

fanc(path,before,later):

import

os with open(

'path

','rb

')as f1,\

open(

'b.txt

','wb

') as f2:

for x in

f1: f2.write(x.replace(before,later))

os.remove(path)

os.rename(

'b.txt

',path)

fanc(

'a.txt

','a

','b

')

2、寫函式,計算傳入字串中【數字】、【字母】、【空格] 以及 【其他】的個數

def count(*args):

dic_num=dict

for n in *args:

ifn.isdigit():

dic_num[

'nember

']+=1:

elif

n.isalpha():

dic_num[alpha] += 1:

elif

n.isspace():

dic_num[space] += 1:

else

: dic_num[other] += 1:

print

(dic_num)

count(*args)

3、寫函式,判斷使用者傳入的物件(字串、列表、元組)長度是否大於5。

def

length(user_inp):

if len(user_inp) > 5:

print('

長度大於5')

else

:

print('

長度小於5')

user_inp = input('

請輸入:')

length(user_inp)

4、寫函式,檢查傳入列表的長度,如果大於2,那麼僅保留前兩個長度的內容,並將新內容返回給呼叫者。

def

new_list(list1):

if len(list1) > 2:

list1 = list1[:2]

return

list1

else

:

return

'列表長度小於2

'list1 = [1,2,3]

res =new_list(list1)

print(res)

5、寫函式,檢查獲取傳入列表或元組物件的所有奇數字索引對應的元素,並將其作為新列表返回給呼叫者。

def

new_list(list1):

list2 =

for i,v in

enumerate(list1):

if i % 2 == 1:

return

list2

list1 = [1,2,3,4]

res =new_list(list1)

print(res)

6、寫函式,檢查字典的每乙個value的長度,如果大於2,那麼僅保留前兩個長度的內容,並將新內容返回給呼叫者。

dic =

ps:字典中的value只能是字串或列表

def

dic_new(dic):

for key in

dic:

if len(dic[key]) > 2:

dic[key] = dic[key][:2]

return

dicdic =

res =dic_new(dic)

print(res)

3月14號筆記

jquery 用處 簡化j ascript的碼字量。換言之,不會js,學jquery 腦子有坑。原理 它封裝j ascript常用的功能 提供一種簡便的j ascript設計方式。ajax 用處 不用重新整理頁面,完成與後台互動。原理 new乙個xmlhttprequest物件和後台互動,再把結果傳...

作業3月25號

1 檔案內容如下,標題為 姓名,性別,年紀,薪資 egon male 18 3000 alex male 38 30000 wupeiqi female 28 20000 yuanhao female 28 10000 要求 從檔案中取出每一條記錄放入列表中,列表的每個元素都是的形式 dic lis...

作業3月24號

1 編寫課上講解的有參裝飾器準備明天默寫 def auth db type defdeco func name input your name strip pwd input your password strip if db type file print 基於檔案的驗證 if name egon...