日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

4道关于Python函数的练习题

發布時間:2025/3/20 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4道关于Python函数的练习题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.寫函數,計算傳入函數的字符串中數字、字母、以及其他的個數

def count_fuc(str):count_number=0count_a=0count_b=0for i in str:if i.isdecimal():count_number+=1elif i.isalpha():count_a+=1else:count_b+=1return print("數字:%s,字母:%s,其他:%s"%(count_number,count_a,count_b)) str="fdsfdsf322@" count_fuc(str) ### 數字:3,字母:7,其他:1

2.寫一個函數,此函數只接收一個參數且參數必須是列表數據類型,此函數的完成功能是返回給調用者一個字典。

eg:傳入的列表為[1,2,3,4],返回的字典為{0:1,1:2,2:3,3:4}

def fun(list1):dict1={}for i in range(len(list1)):dict1[i]=list1[i]return dict1 list1=[1,2,3,4] print(fun(list1))

3.寫函數,函數接收四個參數分別是:姓名,性別,年齡,學歷,用戶通過這四個內容傳送到函數中

此函數接受這個這個內容并追加到student_messgae.txt這個文本中,支持用戶持續輸入,Q或者q退出

''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' def fuc(name,sex,age,qualifications):with open("student_message.txt",encoding="utf-8",mode="a") as f:f.write("%s,%s,%s,%s\n"%(name,sex,age,qualifications)) while True:name=input("please input your name:")if name.upper()=="Q":breakelse:sex=input("please input your sex:")age = input("please input your age:")qualifications= input("please input your qualifications:")fuc(name,sex,age,qualifications)

4.寫函數,用戶傳入修改的文件名,與要修改的內容,執行函數,完成整個函數的修改操作。

import os def refile(file1,oldcontent,newcontent):with open(file1,encoding="utf-8") as f,\open("new{}".format(file1),encoding="utf-8",mode="w") as f1:for line in f:content=line.replace(oldcontent,newcontent)f1.write(content)# old_content=f.read()# new_content=old_content.replace(oldcontent,newcontent)# f1.write(new_content)os.remove(file1)os.rename("new{}".format(file1),file1) refile("student_message.txt","房貸首付","沒錢")

結尾給大家推薦一個非常好的學習教程,希望對你學習Python有幫助!

Python基礎入門教程推薦:更多Python視頻教程-關注B站:Python學習者

Python爬蟲案例教程推薦:更多Python視頻教程-關注B站:Python學習者

總結

以上是生活随笔為你收集整理的4道关于Python函数的练习题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。