」』
1、函式的定義:將可重複使用的,實現某種功能的**段組織在一起
2、函式的語法:
def 函式名(引數):
函式體
return
2.1、函式名是乙個變數,因此命名規則需遵守變數命名規則
3、函式的呼叫
函式名()
4、函式的返回值
return 可空、none、單個、多個以tuple返回給呼叫者
5、函式的引數:
函式定義時宣告的變數的叫形參
1、位置引數
2、預設引數
函式呼叫時傳給函式的叫實參
1、位置引數
定義多少個,呼叫時也必須傳多少個
2、關鍵字引數
3、混合引數
位置引數需在關鍵字引數之前
print('first'.center(40, '='))
defa3
(arg):
ret = [ ]
for i in range(len(arg)):
if i % 2 == 1:
else:
pass
return ret
li = [11,22,33,44,55]
r = a3(li)
print(li)
print(r)
print('first'.center(40, '='))
deffun1
(n):
print('判斷傳入物件的長度是否大於5')
if len(n) >= 5:
return
true
else:
return
false
content = input('請輸入內容:')
print(fun1(content))
dic =
defa4
(arg):
ret = {}
for key, value in arg.items():
if len(value) > 2:
ret[key] = value[0:2]
else:
ret[key] = value
return ret
r = a4(dic)
print(r)
def
count
(s):
dict =
for i in s:
if i.isdigit():
dict['數字的個數:'] += 1
elif i.islower() or i.isupper():
dict['字母的個數:'] += 1
elif i.isspace():
dict['空格的個數:'] += 1
else:
dict['其他的個數:'] += 1
return dict
str = input('請輸入內容:')
res = count(str)
print(res)
注漢字isalpha判斷它為字母
print('first'.center(40, '='))
defnum
(a,b):
if a > b:
return a
else:
return b
r = num(1,2)
print(r)
dic =
defa4
(arg):
ret = {}
for key, value in arg.items():
if len(value) > 2:
ret[key] = value[0:2]
else:
ret[key] = value
return ret
r = a4(dic)
print(r)
def ret_dic(l):
dic = {}
if type(l) == list: # 判斷型別,如果用is not 判斷的是記憶體位址
for k, v in enumerate(l):
dic[k] = v
return dic
# 避免為空,return必須要返回的是個物件,故需要把return放帶外面
# 如果傳給的是空列表或其他型別的資料,不能返回none
def ret_dic2(lis):
dic = {}
if type(lis) == list:
for index in lis:
dic[index] = lis[index]
return dic # *******important
def input_file(name, ***, age, education):
with open(「student_msg」, 「a」, encoding=」utf-8」) as f:
li = 「\t」.join([name, ***, age, education])
f.write(li+」\n」)
name = input(「請輸入您的姓名:」)
*** = input(「請輸入您的性別:」)
age = input(「請輸入您的年齡:」)
education = input(「請輸入您的學歷:」)
input_file(name, ***, age, education)
def
input_file
(name,age, education,***="男"):
with open(name, "a+", encoding= "utf-8" ) as f:
s = "\t".join([name, ***, age, education])
f.write(s +"\n")
# nr=s +"\n"
# f.write(nr)
while
1: name = input("請輸入您的姓名(輸入q或q退出):")
if name.strip().upper() == "q":
break
else:
*** = input("請輸入您的性別(預設為男,回車即可):")
age = input("請輸入您的年齡:")
education = input("請輸入您的學歷:")
if *** == "女":
input_file(name,age, education,*** )
else:
input_file(name,age, education)
print("輸入成功")
break
def change_file(file_name, old_mesg, chang_mesg):
with open(「file_name」,」r」,encoding=」utf-8」) as f1,\
open(「temp_file」,」w」,encoding=」utf-8」) as f2:
for line in f1:
line = line.replace(old_mesg, chang_mesg)
f2.write(line)
import os
os.remove(file_name)
os.rename(「temp_file」, file_name)
user_name = 13321
password = 133211
deflogin
():for i in range(0,3):
name = int(input('請輸入賬號:'))
if name == user_name:
psw = int(input('請輸入密碼:'))
if psw == password:
print('登入成功'.center(40, '='))
return
else:
print('密碼錯誤'.center(40, '='))
if i == 2:
print('您的機會已用完,請12小時之後再試')
else:
print('您還有%s次機會' % (2 - i))
else:
print('賬號不存在'.center(40, '='))
if i == 2:
print('您的機會已用完,請12小時之後再試')
else:
print('您還有%s次機會' % (2 - i))
res = login()
defregist
():while
1: print('請註冊'.center(40, '='))
re_name = int(input('請輸入賬號:'))
if re_name == user_name:
print(''.center(40, '='))
else:
re_psw = int(input('請輸入密碼:'))
return
'註冊成功'.center(40, '=')
re = regist()
print(re)
python函式 練習題
1.寫函式,使用者傳入修改的檔名,與要修改的內容,執行函式,完成整個檔案的批量修改操作import os def modify file file name,content,new str new filename sfile name new.new file open new filename,...
python書中練習題 python練習題
1 定義乙個空列表,接收從鍵盤輸入的整數,把列表傳給乙個從大到小排序的函式,再輸出排序後的列表的值 listex b 0 a int input 請輸入列表長度 while b a num int input 請輸入字元 b 1 print listex sum 0 for i in range 0...
python的練習題 Python練習題
1 使用while迴圈輸入1 2 3 4 5 6 8 9 10 i 0while i 10 i i 1 if i 7 continue print i 結果 e python python python test.py1 2 求1 100的所有數的和 i 0sum 0 while i 100 i 1...