1. 程式人生 > >python學習第四天控制流程if、while、for

python學習第四天控制流程if、while、for

and code als 字符串 con image 控制流 please inpu

一、if

1 什麽是if判斷
判斷一個條件如果成立則做...不成立則做....
2 為何要有if判斷
讓計算機能夠像人一樣具有判斷的能力
3、如何使用if判斷
(1)if 條件1:
code1
code2
.....
(2)if 條件1:
code1
code2
.....
else
code1
code2
.....
(3)if 條件1:
if 條件2:
code1
code2
code3
code4
(4)if 條件1:
code1
elif 條件2:
code2
elif 條件3:
code3
elif 條件4:
code4
技術分享圖片

二、while

1. 什麽是循環
循環指的是一個重復做某件事的過程
2. 為何要有循環
為了讓計算機能夠像人一樣重復做某件事
3. 如何用循環
while循環的語法:while循環又稱為條件循環,循環的次數取決於條件
while 條件:
code1
code2
技術分享圖片
4、如何結束while循環
(1)操作while循環條件讓其結束
例一
print(‘start..‘)
tag=True
while tag:
name=input(‘please your name>>: ‘)
pwd=input(‘please your password>>: ‘)
if name==‘egon‘ and pwd==‘123‘:
print(‘login successful‘)
tag=False
else:
print(‘user or password err‘)
print(end..)
例二
count=1
while count < 6:
print(count)
count+=1
(2)break強行終止本層循環
例一
print(‘start..‘)
while True:
name=input(‘please your name>>: ‘)
pwd=input(‘please your password>>: ‘)
if name==‘egon‘ and pwd==‘123‘:
print(‘login successful‘)
break
else:
print(‘user or password err‘)
print(end..)
例二

count=1
while True
if count > 5:
break
print(count)
count+=1
(3)假設如何設置輸錯三次就退出
方式一:
print(‘start...‘)
count=0
while count <=2:
name=input(‘please your name>>: ‘)
pwd=input(‘please your name>>: ‘)
if name ==‘egon‘ and pwd ==‘123‘:
print(‘login successful‘)
break
else:
print(‘user or password err‘)
count+=1
print(‘end..‘)
方式二
技術分享圖片
5、continue代表結束本次循環,直接進入下一次
技術分享圖片

ps:不能將continue作為循環體最後一步執行的代碼

6、while+else

技術分享圖片

輸錯三次則退出之while+else的應用

技術分享圖片

7、while循環的嵌套

name_of_db=egon
pwd_of_db=123
print(start....)
count=0
while count <= 2: #count=3
    name=input(please your name>>: )
    pwd=input(please your password>>: )
    if name == name_of_db and pwd == pwd_of_db:
        print(login successful)
        while True:
            print("""
            1 瀏覽商品
            2 添加購物車
            3 支付
            4 退出
            """)
            choice=input(請輸入你的操作: ) #choice=‘1‘
            if choice == 1:
                print(開始瀏覽商品....)
            elif choice == 2:
                print(正在添加購物車....)
            elif choice == 3:
                print(正在支付....)
            elif choice == 4:
                break
        break
    else:
        print(user or password err)
        count+=1
else:
    print(輸錯的次數過多)

print(end...)

8、tag控制所有的while循環

name_of_db=egon
pwd_of_db=123
tag=True
print(start....)
count=0
while tag:
    if count == 3:
        print(嘗試次數過多)
        break
    name=input(please your name>>: )
    pwd=input(please your password>>: )
    if name == name_of_db and pwd == pwd_of_db:
        print(login successful)
        while tag:
            print("""
            1 瀏覽商品
            2 添加購物車
            3 支付
            4 退出
            """)
            choice=input(請輸入你的操作: ) #choice=‘1‘
            if choice == 1:
                print(開始瀏覽商品....)
            elif choice == 2:
                print(正在添加購物車....)
            elif choice == 3:
                print(正在支付....)
            elif choice == 4:
                tag=False

    else:
        print(user or password err)
        count+=1

print(end...)

三、for

for循環主要用於循環取值
student=[egon,虎老師,lxxdsb,alexdsb,wupeiqisb]

1、如果不用for取值,用while



i=0
while i < len(student):
    print(student[i])
    i+=1

2、用for取值列表



for item in student:
    print(item)

3、用for取值字符串



for item in hello:
    print(item)

4、用for取值字典

dic={x:444,y:333,z:555}
for k in dic:
    print(k,dic[k])

5、用for取值range



for i in range(1,10,3):
    print(i)

range(1,10,3)指1到10之間去的整數不包括10,3是指間隔,輸出結果是1,4,7







 


 

技術分享圖片

python學習第四天控制流程if、while、for