Python 入門基礎3 ---流程控制
阿新 • • 發佈:2019-03-21
很遺憾 hide 乘法口訣表 寫入 代碼 什麽 空格 open 換行
一、流程控制--if
1.if判斷:
# if判斷 age = 21 weight = 50 if age > 18 and age < 25 and weight >40 and weight < 60: print("表白。。。")View Code
2.if+else:
# 語法二:if - else age = 21 weight = 50 if age > 18 and age < 25 and weight > 40 and weight < 60: print("表白。。。View Code") else: print("你好!再見!")
3.if嵌套:
# 語法三:if條件的嵌套 sex = "female" age = 18 weight = 50 is_successful = True if sex == "female" and age >= 18 and age < 25 and weight > 40 and weight < 60 : print("表白。。。") if is_successful: print("在一起、、、、View Code") else: print("qtmd、、、、、") else: print("你好!再見!")
4.if-elif:
如果 成績>=90,那麽:優秀 如果 成績>=80且<90,那麽:良好 如果 成績>=70且<80,那麽:普通 其他情況:很差
score = input("請輸入你的成績:") score = int(score) if score >= 90: # print("優秀!") print("你真TM優秀!")View Codeelif score >= 80: # print("良好!") print("恩,還不錯!") elif score >= 70: # print("普通!") print("一般般啦!") else: # print("很差!") print("回家賣紅薯吧!")
二、流程控制--while
1.while循環又稱為條件循環
while True: name = input("Please input your name :") pwd = input("Please input your password :") if name == "zhangsan" and pwd =="123": print("login。。。。。。") else: print("Your username or password error , please input again!")View Code
2.while+條件結束
# ---條件跳出-- tag = True while tag: name = input("Please input your name :") pwd = input("Please input your password :") if name == "zhangsan" and pwd =="123": print("login。。。。。。") tag = False else: print("Your username or password error , please input again!")View Code
3.while+break:
break:退出本層循環
# ---break----- while True: name = input("Please input your name :") pwd = input("Please input your password :") if name == "zhangsan" and pwd =="123": print("login。。。。。。") break else: print("Your username or password error , please input again!")View Code
4.while+continue:
continue:退出本次循環,繼續下一次循環
# ---continue---- count = 1 while count < 6: if count == 4: count += 1 continue print(count) count += 1View Code
5.while + else
當while循環正常執行完,中間沒有break中止的話,執行else
# ---while-else---- count = 1 while count < 6: if count == 4: count += 1 continue print(count) count += 1 else: print("while-else最後else的代碼")View Code
三、流程控制--for
for循環又稱為叠代循環
for可以不依賴於索引取指,是一種通用的循環取指方式
循環次數是由被循環對象包含值的個數決定的,而while的循環次數是由條件決定的
for i in range(10): print(i)View Code
1.for+break:
和while類似
2.for+continue:
和while類似
3.for+else:
和while類似
4.range():
range(1,5) >>>1 2 3 4 range(5) >>>1 2 3 4 range(1,5,2) #i = i + 2 >>>1 3
註意:
python2中:range(1,5)直接生成[1,2,3,4]列表
python3中:range(1,5)不會直接生成列表,依舊是 range(1,5)
5.嵌套循環
# for嵌套循環 for i in range(3): for j in range(3): print(i,j) 結果: 0 0 0 1 0 2 1 0 1 1 1 2 2 0 2 1 2 2View Code
本節練習:
一、while循環
1. 使用while循環輸出1 2 3 4 5 6 8 9 102. 求1-100的所有數的和
3. 輸出 1-100 內的所有奇數
4. 輸出 1-100 內的所有偶數
5. 求1-2+3-4+5 ... 99的所有數的和
6. 用戶登陸(三次機會重試)
7:猜年齡遊戲
要求:
允許用戶最多嘗試3次,3次都沒猜對的話,就直接退出,如果猜對了,打印恭喜信息並退出
8:猜年齡遊戲升級版
要求:
允許用戶最多嘗試3次
每嘗試3次後,如果還沒猜對,就問用戶是否還想繼續玩,如果回答Y或y, 就繼續讓其猜3次,以此往復,如果回答N或n,就退出程序
如何猜對了,就直接退出
# 1.使用while循環輸出1 2 3 4 5 6 8 9 10 i = 1 while i < 10: if i == 7: i += 1 continue print(i,end=‘ ‘) i += 1View Code
#2. 求1-100的所有數的和 sum = 0 i = 1 while i<=100: sum +=i i +=1 print(sum)View Code
#3.輸出 1-100 內的所有奇數 i = 1 while i<=100: print("%s"%i,end=" ") i += 2View Code
#4.使用 while 循環實現輸出 1-100 內的所有偶數 i = 1 while i<=100: print("%s" % (i+1), end=" ") i += 2View Code
# 5.使用while循環實現輸出2-3+4-5+6...+100 的和 i = 2 sum = 0 tag = True # 符號判斷位,用來控制加減 True: +i False: -i while i <= 100: if tag: sum += i tag = False else: sum -= i tag = True i += 1 print("2-3+4-5+...+100 = %s" % sum)View Code
#6. 用戶登陸(三次機會重試) count = 0 tag = True while tag: if count >= 3: break name = input("Please input your name :") pwd = input("Please input your password :") if name == "zhangsan" and pwd =="123": print("login。。。。。。") tag = False else: print("Your username or password error , please input again!") count += 1View Code
# #7:猜年齡遊戲 # 要求: # 允許用戶最多嘗試3次,3次都沒猜對的話,就直接退出,如果猜對了,打印恭喜信息並退出 AGE = 56 tag = True count = 0 while tag: age = input("請猜一猜年齡:") if age == AGE: print("這都能猜中!太厲害了!") tag = False else: print("很遺憾,你猜錯了!") if count ==2: print("你都猜了三次了,竟然還沒猜對!!") break count +=1View Code
# #8:猜年齡遊戲升級版 # 要求: # 允許用戶最多嘗試3次 # 每嘗試3次後,如果還沒猜對,就問用戶是否還想繼續玩,如果回答Y或y, 就繼續讓其猜3次,以此往復,如果回答N或n,就退出程序 # 如何猜對了,就直接退出 AGE = 56 tag = True count = 0 while tag: age = input("請猜一猜年齡:") if age == AGE: print("這都能猜中!太厲害了!") tag = False else: print("很遺憾,你猜錯了!") count += 1 if count ==3: is_continue = input("你還要繼續猜嗎?(Y/N) :") if is_continue == ‘Y‘ or is_continue == ‘y‘: count = 0 else: tag = FalseView Code
二、for循環
1.打印九九乘法口訣表
以下是 print() 方法的語法: print(*objects, sep=‘ ‘, end=‘\n‘, file=sys.stdout) 參數 objects -- 復數,表示可以一次輸出多個對象。輸出多個對象時,需要用 , 分隔。 sep -- 用來間隔多個對象,默認值是一個空格。 end -- 用來設定以什麽結尾。默認值是換行符 \n,我們可以換成其他字符串。 file -- 要寫入的文件對象。
輸出結果:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
for i in range(1, 10): for j in range(1, i + 1): print("%s*%s=%s" % (i, j, i * j), end=‘ ‘) print() # 內循環進行完一次,換行View Code
2.打印金字塔
輸出結果:
* *** ***** ******* *********
# 打印金字塔 row = 5 col = 9 for i in range(1,row+1): # 5行 num = (2*i-1) # 此行將打印幾個 * for l in range((col-num)//2): # 打印前面幾個空格符 print(" ",end="") for k in range((col-num)//2,(col-num)//2 + num): # 在哪個位置開始打印 * print("*",end=‘‘) print()View Code
Python 入門基礎3 ---流程控制