Python学习笔记--8.6 函数--递归
生活随笔
收集整理的這篇文章主要介紹了
Python学习笔记--8.6 函数--递归
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#遞歸:函數自己調用自己
#遞歸最多遞歸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')
#遞歸最多遞歸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')
轉載于:https://www.cnblogs.com/youyou-luming/p/9649536.html
總結
以上是生活随笔為你收集整理的Python学习笔记--8.6 函数--递归的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ACM-ICPC 2018徐州网络赛-H
- 下一篇: 关于MQTT、HTTP、WebServi