python while if else_python學習筆記(if else流程判斷、while循環、for循環)

if else流程判斷

getpass在pycharm中無法使用,在命令行窗口中進入python環境可以使用。

import getpass

username = input("username:")password = getpass.getpass("password:")

print(username,password)

python中縮進錯誤:

為什么python中強制縮進,因為python中不需要定義結束符。省去了結束符,子代碼強制縮進讓結構變得更清晰。

最外層代碼必須頂格寫,不然就會報縮進錯誤。

if else基礎程序舉例:

實例一:判斷用戶名密碼是否正確

9c49bbeafd05eb8f4c9ebe2d811877dd.gif_username = 'alex'

_password = 'abc123'

username = input("username:")

password = input("password:")

if _username == username and _password == password:

print("Welcom user {name} login...".format(name=username))

else:

print("Ivalid username or password")9c49bbeafd05eb8f4c9ebe2d811877dd.gif

實例二:猜年齡

9c49bbeafd05eb8f4c9ebe2d811877dd.gif# 猜年齡

age_of_oldboy = 56

guess_age = int(input("guess age:"))

if guess_age == age_of_oldboy:

print("you got it!")

elif guess_age < age_of_oldboy:

print("think bigger...")

else:

print("think smaller...")9c49bbeafd05eb8f4c9ebe2d811877dd.gif

while循環

#最簡單的while循環程序舉例

count = 0

while True:

print("count:",count)

count = count+1 #相當於count +=1

實例一:猜年齡

9c49bbeafd05eb8f4c9ebe2d811877dd.gif#猜年齡,共猜3次,如果3次內猜對也會結束程序

age_of_oldboy = 56

count = 0

while True:

if count == 3:

break

guess_age = int(input("guess age:"))

if guess_age == age_of_oldboy:

print("you got it!")

break

elif guess_age < age_of_oldboy:

print("think bigger...")

else:

print("think smaller...")

count +=19c49bbeafd05eb8f4c9ebe2d811877dd.gif

實例二:對實例一代碼的優化

9c49bbeafd05eb8f4c9ebe2d811877dd.gif#猜年齡,共猜3次,如果3次內猜對也會結束程序

age_of_oldboy = 56

count = 0

while count < 3:

guess_age = int(input("guess age:"))

if guess_age == age_of_oldboy:

print("you got it!")

break

elif guess_age < age_of_oldboy:

print("think bigger...")

else:

print("think smaller...")

count +=19c49bbeafd05eb8f4c9ebe2d811877dd.gif

實例三:增加人性化提示,輸入3次錯誤密碼后會得到提示:嘗試太多次了。

9c49bbeafd05eb8f4c9ebe2d811877dd.gif#猜年齡,共猜3次,如果3次內猜對也會結束程序,嘗試3次后得到提示:你嘗試的次數過多。

age_of_oldboy = 56

count = 0

while count < 3:

guess_age = int(input("guess age:"))

if guess_age == age_of_oldboy:

print("you got it!")

break

elif guess_age < age_of_oldboy:

print("think bigger...")

else:

print("think smaller...")

count +=1

if count == 3:

print("you have tried too many times...")9c49bbeafd05eb8f4c9ebe2d811877dd.gif

實例四:對實例三程序的優化。提示代碼的判斷可以直接用else。

9c49bbeafd05eb8f4c9ebe2d811877dd.gif#猜年齡,共猜3次,如果3次內猜對也會結束程序,嘗試3次后得到提示:你嘗試的次數過多。

age_of_oldboy = 56

count = 0

while count < 3:

guess_age = int(input("guess age:"))

if guess_age == age_of_oldboy:

print("you got it!")

break

elif guess_age < age_of_oldboy:

print("think bigger...")

else:

print("think smaller...")

count +=1

else:

print("you have tried too many times...")9c49bbeafd05eb8f4c9ebe2d811877dd.gif

for循環

實例一,最簡單的for循環程序

for i in range(10):

print("loop",i)

等於以下:

等於以下:

for i in range(0,10,1): #步長默認為1

print("loop",i)

i,臨時變量

range,相當於定義了(0,1,2,3,4,5,6,7,8,9) 每循環一次i按順序取值一次。

實例二:上節課中的while循環實例改為for循環:

9c49bbeafd05eb8f4c9ebe2d811877dd.gif#猜年齡,共猜3次,如果3次內猜對也會結束程序,嘗試3次后得到提示:你嘗試的次數過多。

age_of_oldboy = 56

for i in range(3):

guess_age = int(input("guess age:"))

if guess_age == age_of_oldboy:

print("you got it!")

break

elif guess_age < age_of_oldboy:

print("think bigger...")

else:

print("think smaller...")

else:

print("you have tried too many times...")9c49bbeafd05eb8f4c9ebe2d811877dd.gif

實例三,打印10以內的偶數:

for i in range(0,10,2): #2為步長

print("loop",i)

實例四,優化while猜年齡程序

9c49bbeafd05eb8f4c9ebe2d811877dd.gif#猜年齡,共猜3次,嘗試3次后詢問是否繼續,如果回答:n,則結束程序;其他則重新開始程序。

age_of_oldboy = 56

count = 0

while count < 3:

guess_age = int(input("guess age:"))

if guess_age == age_of_oldboy:

print("you got it!")

break

elif guess_age < age_of_oldboy:

print("think bigger...")

else:

print("think smaller...")

count +=1

if count ==3:

continue_confirm = input("do you want to keep guessing?")

if continue_confirm != "n":

count = 09c49bbeafd05eb8f4c9ebe2d811877dd.gif

break和continue的區別,根據下面2段代碼,使用debug調試功能在pycharm中運行,觀察后得知

代碼一:

9c49bbeafd05eb8f4c9ebe2d811877dd.gif# continue的作用是結束本次循環,不會終止for循環

for i in range(0,10):

if i <3:

print("loop",i)

else:

continue

print("hehe...")9c49bbeafd05eb8f4c9ebe2d811877dd.gif

代碼二:

9c49bbeafd05eb8f4c9ebe2d811877dd.gif# break是結束當前循環

for i in range(0,10):

if i <3:

print("loop",i)

else:

break

print("hehe...")9c49bbeafd05eb8f4c9ebe2d811877dd.gif

循環嵌套

for i in range(0,10):

print("--------",i)

for j in range(10):

print(j)

if j >5:

break

查看輸出:小循環輸出0-6,大循環輸出0-9,brake只中斷當前循環。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值