python學習--流程控制和循環控制(while for if)
阿新 • • 發佈:2018-08-01
wan tar === 2個 clas 範圍 案例 有一個 生成
一、流程控制語句(if)
1. if 判斷
‘‘‘ 流程控制語句 @date:2018-07-31 if... if...else... if...elif...elif.. else.... ‘‘‘ # if.. ‘‘‘ if 表達式(非空(空對象、空字符串),非零 為 True): 代碼塊 ‘‘‘ if ‘1‘: print(‘info............‘) pass #從控制臺輸入的你的名字 如果名字長度小於等於4個 大於等於2個 輸出名字 name=input("請輸入你的中文名:") if len(name)<=4 and len(name)>=2:print(name) print(‘程序結束‘) ‘‘‘ if...else... if 表達式(非空(空對象、空字符串),非零 為 True): 程序塊 else: 程序塊 if。。。eles 中的代碼塊只能執行一塊。。。 ‘‘‘ #如果你的年齡大於等於18 代表成年人 否則:未成年 age=int(input("請輸入你的年齡:\n")) if age>=18: print("你已成年。。。。。。") else: print("未成年") print("程序結束") ‘‘‘if...elif...elif...else if 表達式(非空(空對象、空字符串),非零 為 True): 程序塊 elif 表達式 表達式(非空(空對象、空字符串),非零 為 True): 代碼塊 --此處elif可以有多個 else: 程序塊‘‘‘
運行:【根據輸入判斷,進行不同輸出】
============== RESTART: E:\python練習\day5\案例\demo0731\demo04.py ============== info............ 請輸入你的中文名:李欣 李欣 程序結束 請輸入你的年齡: 22 你已成年。。。。。。 程序結束
2.實例:
1.
‘‘‘ 輸入性別 輸出性別 ‘‘‘ info_input = input(‘輸入性別:\n ‘) if info_input == ‘男‘: print(‘你是%s‘%info_input) print(‘你是%s‘%info_input)‘‘‘ 輸入手機號, 要求:純數字,長度11位 ‘‘‘ phone_input = input(‘Phone: ‘) if phone_input.isdigit() == True and len(phone_input) == 11: print(‘Your cell phone number is:‘,phone_input) else: print(‘Error‘) ‘‘‘ 輸入年齡,年齡範圍:0-12兒童 12-18少年 18-25 青少年 25-40青年 40-60中年 60老年 ‘‘‘ age_input = input(‘你的年齡是:\n‘) if int(age_input) >= 60: print(‘老年人‘) elif int(age_input) >= 40: print(‘中年人‘) elif int(age_input) >= 25: print(‘青年人‘) elif int(age_input) >= 18: print(‘青少年‘) elif int(age_input) >= 12: print(‘少年‘) else: print(‘兒童‘)
運行:【根據輸入判斷,進行不同輸出】
E:\python_VS_code\directory[目錄]>D://py3.6//python.exe e:/python_VS_code/directory[目錄]/demo0731/py_if.py 輸入性別: 男 你是男 你是男 Phone: 122 Error 你的年齡是: 22 青少年
2.
phone=input("請輸入你手機號的前三位") if len(phone)==3 and phone.isdigit(): if ‘135‘ ==phone or phone==‘155‘ or phone==‘150‘ or phone==‘138‘ or phone==‘139‘ : print("你當前的手機號是中國移動的號碼") elif ‘132‘==phone or ‘130‘==phone : print("你當前的手機號是中國聯通的號碼") elif ‘189‘==phone or ‘153‘==phone or ‘177‘==phone: print(‘中國電信‘) else: print(‘當前手機號前三位無法查詢到運營商‘) else: print("手機號前三位無效")
運行:【根據輸入判斷,進行不同輸出】
請輸入你手機號的前三位qwe
手機號前三位無效
-----------------------------------------------------------------------------------------------------------------------------------------------
二、循環控制語句1
1.while 循環
‘‘‘ 流程控制語句之循環語句 while 循環 while true: 循環體 ‘‘‘ # while 1: # print(‘1111111111‘) i=0 while i<=9: print("循環") i+=1 i=20 #控制循環執行10次 while i<30: print(i) i+=1 # 1-100的累加和 sum=0 i=1 while i<=100: sum+=i i+=1 pass print(‘1---100的累加和‘,sum) while i==101: print(‘----------‘) i+=1 else: # 循環結束會執行(必須執行) print("循環結束") pass # break(結束整個循環) continue(結束本次循環、循環繼續執行) # 輸出1-100中的偶數 num=0 while num<=100:#用於生成1-100的數 #判斷是否是偶數 num+=1 if num%2!=0: # num+=1 continue #當執行continue continue之後程序不再執行 當前所在循環體 print(num) print(‘1111111111‘) #break 使用 跳出當前循環 當前循環結束 #定義列表集 存儲用戶賬號信息 usersList=[‘wm001‘,‘houshan001‘,‘xiewanni‘,‘chengshaoyan‘,‘lifengyuan‘] #從控制臺接收賬號信息,判斷是否存在於列表集中存在輸出 已存在 (拒絕使用in) index=0 account=input("請輸入你的賬號信息") while index<len(usersList): print(usersList[index]) if usersList[index]==account: print("賬號已存在") break #結束循環體,循環結束 index+=1
運行:【沒有條件限制,會變成死循環】
============== RESTART: E:\python練習\day5\案例\demo0731\demo07.py ============== 循環 循環 循環 循環 循環 循環 循環 循環 循環 循環 20 21 22 23 24 25 26 27 28 29 1---100的累加和 5050 ---------- 循環結束 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 1111111111 請輸入你的賬號信息lifeng wm001 houshan001 xiewanni chengshaoyan lifengyuan **************************************************************************************************** 請輸入你的賬號:wm00 請輸入你的賬號:wm001 請輸入你的賬號:000
2.實例
1.
‘‘‘ 登錄三次 ‘‘‘ count=0 # 基礎登陸次數 usersList=[‘wm001‘,‘houshan001‘,‘xiewanni‘,‘chengshaoyan‘,‘lifengyuan‘] while True: #控制賬號輸入 if count>2:# 賬號輸入次數為3次 break index=0#用戶賬號列表的索引 account=input("請輸入你的賬號信息") count+=1 isLogin=False#是否登陸成功 while index<len(usersList): # print(usersList[index]) if usersList[index]==account: # print("賬號已存在") isLogin=True break #結束循環體,循環結束 index+=1 if isLogin: print(‘登陸成功‘) break # 登陸成功 退出登錄輸入循環 else: print("賬號不存在")
運行:【沒有條件限制,會變成死循環】
============== RESTART: E:\python練習\day5\案例\demo0731\demo08.py ==============
請輸入你的賬號信息222
賬號不存在
請輸入你的賬號信息lifengyuan
登陸成功
2.
‘‘‘打印1-100中是5倍數, 使用continue ‘‘‘ num1 = 0 while num1 <= 100: num1+=1 if num1%5 != 0: continue print(num1) ‘‘‘ 輸入用戶死循環,中間break控制 只能輸入三次帳號 使用break完成 ‘‘‘ num2 = 0 while True: if num2 > 2: break user_name = input(‘請輸入賬號:‘) num2+=1
運行:【沒有條件限制,會變成死循環】
E:\python_VS_code\directory[目錄]>D://py3.6//python.exe e:/python_VS_code/directory[目錄]/demo0731/py_while.py 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 請輸入賬號:da 請輸入賬號:dw 請輸入賬號:dw
三、循環控制語句2
for循環
這裏只有一個實例
代碼:
dict_city = {‘陜西‘:[‘西安‘,‘鹹陽‘,‘榆林‘,‘銅川‘], ‘河南‘:[‘鄭州‘,‘開封‘,‘安陽‘,‘商丘‘], ‘湖北‘:[‘武漢‘,‘黃岡‘,‘周口‘,‘禹州‘]} for i in dict_city.keys(): print(‘----‘,i,‘----‘) for val in dict_city[i]: print(‘|-‘,val)
運行:
E:\python_VS_code\directory[目錄]>D://py3.6//python.exe e:/python_VS_code/directory[目錄]/demo0801/py_for.py ---- 陜西 ---- |- 西安 |- 鹹陽 |- 榆林 |- 銅川 ---- 河南 ---- |- 鄭州 |- 開封 |- 安陽 |- 商丘 ---- 湖北 ---- |- 武漢 |- 黃岡 |- 周口 |- 禹州
python學習--流程控制和循環控制(while for if)