1. 程式人生 > >python的基本流程控制

python的基本流程控制

基本 判斷 als for 語句 ces success 尊重 ESS

一:if判斷語句
1.1 if判斷語法之一
if條件:
子代碼塊

1.2 if判斷語法之二
if條件:
子代碼塊
else:
子代碼塊

1.3 if判斷語法之三
if條件:
if條件:
子代碼塊

1.4 if判斷語法之四
if條件:
子代碼塊
elif條件:
子代碼塊
,,,
else條件:
子代碼塊
舉個簡單例子 如:
sex = ‘male‘
age = 19
is_beauty = True
success = True
if is_beauty and age < 20 and age > 16 and sex ==‘male‘:

if success:
print(‘收獲愛情‘)
else:
print(‘我愛你不後悔 也尊重故事結尾‘)
else:
print(‘你很好,只是我們不合適‘)

二while循環
2.1while條件:while True:
print(‘hello world‘)
2.2結束while循環的兩種方式
1條件改為false
conut=1
while count<2:
print(‘hello world‘)
count+=1
2while+break
while True:
print(‘hello world‘)
break
2.3while+continue
count=1
while count<6:
if count==4:
count+=1
continue
else:
print(count)
count+=1
2.4while+else
while 條件:
代碼1
代碼2
代碼3
else:
在循環結束後,並且在循環沒有被break打斷過的情況下,才會執行else的代碼

2.5while+while
tag = True
while tag:
name = input(‘請輸入你的名字:‘)
pwd = input(‘請輸入密碼:‘)

if name == ‘egon‘ and pwd ==‘123‘:
print(‘login is successful‘)
while tag:
print("""
0是退出
1是存款
2是查詢
3是取款
""")
choice = input(‘請選擇您的操作‘)

if choice ==‘0‘:
print(‘退出‘)
tag = False
elif choice ==‘1‘:
print(‘存款‘)
elif choice ==‘2‘:
print(‘查詢‘)
elif choice == ‘3‘:
print(‘取款‘)
else:
print("""
0是退出
1是存款
2是查詢
3是取款
""")
else:
print(‘username or password is error‘)

三for循環
3.1循環取值
列表與字典
l=[1,2,3,4,5]
for x in l:
print(x)
d={‘name‘:‘egon‘,‘age‘:‘18‘,‘sex‘:‘male‘}
for x in d:
print(d,dic[x])
3.2for+break
nums=[11,22,33,44,55]
for x in nums:
if x==44:
break
print(x)
3.3for+continue
nums=[11,22,33,44,55]
for x in nums:
if x==44:
continue
print(x)
3.4 for+else
3.4 for+range
for i in range(5): #range顧頭不顧尾 range(5)=range(0,5,1)
print(i)

3.5 for嵌套
for i in range(3):
for j in range(4):
print(i,j)

python的基本流程控制