Python學習筆記 8 6 函式 遞迴

2022-09-02 11:30:07 字數 930 閱讀 9520

#遞迴:函式自己呼叫自己

#遞迴最多遞迴999次。

count=0

def say():

global count

count+=1

print('say')

print(count)

say()

say()#自己呼叫自己死迴圈,最多列印999次

#用遞迴迴圈 (能用迴圈時不要用遞迴,因為遞迴的效率不高。)

def test1():

num = int(input('please enter a number:'))

if num % 2 == 0: # 判斷輸入的數字是不是偶數

return true # 如果是偶數的話,程式就退出了,返回true

print('不是偶數請重新輸入!')

return test1() # 如果不是偶數的話繼續呼叫自己,輸入值

print(test1()) # 呼叫test

def db_connect(ip,user,password,db,port):

print(ip)

print(user)

print(password)

print(db)

print(port)

db_connect(user='abc',port=3306,db=1,ip='sdfasdfasd',password='sdfsafaaadfs')

db_connect('192','root',db=2,password='sdfewrwe',port=123)

#前兩種可以用,指定的對應指定內容,沒有指定的按順序賦值。但是混搭時,不能用第三種,沒有指定的不能放後邊或者中間,必須放前面。

db_connect(db=2,password='sdfewrwe','192','root')

Python學習筆記 函式

1.基本呼叫 python 中的函式使用關鍵字 def 來建立乙個函式,正如其他語言中的函式一樣,有函式名,引數,以及返回值。函式的引數不用指定型別,return 可以在任何地方出現,表示函式結束,如果沒有返回內容則預設返回值為none。乙個簡單的無引數,無返回值的hello world def h...

python學習筆記 函式

def fun object,param1,param2 none print type object tuple,呼叫時預設的所有實參全部轉化為tuple傳遞給object fun 1,2,3,4,5,6,7,param1 8 指定param1的呼叫實參,param2引數呼叫預設值函式內可訪問全域...

python學習筆記 函式

建立函式 def myfirstfuntion 函式具體內容 呼叫函式 直接輸入函式面名及引數。def myfirstfnuncyion syntaxerror invalid syntax def myfirstfunction print 我愛你,qt syntaxerror eol while...