Python_循環判斷表達式


一、if判斷

  計算機之所以能做很多自動化的任務,因為它可以自己做條件判斷。

if判斷結構:

if 條件:
    動作
elif 條件:
    動作
else:
    動作

 

 if判斷年齡:

age_of_princal = 56
guess_age = int(input(">>:"))

if age_of_princal == guess_age:
    print("Yes, you got it...")
elif guess_age < age_of_princal:
    print("No, you shoud try biger..")
else:
    print("No,you shoud try smaller..")

 

 

二、for循環

此循環為有限循環,有次數限制。

for循環結構:

for i in ...:
    動作

打印0、1、2三個數

for i in range(3):
    print(i)

打印偶數

for i in range(10):
    if i % 2 == 0:
        print(i)

 for循環編寫登陸程序:

編寫一個登陸程序,允許用戶登陸三次,如果三次輸入錯誤,退出程序。

_use = "Mu"
_passwd = "admin"

for i in range(3):
    username = input("Username: ")
    password = input("Password: ")
    if username == _use and password == _passwd:
        print("Welcome %s login..." % username)
        break
    else:
        print("Username or Password is Falst")
else:
    print("test login too many!!!")

 

 

else的用法

當循環正常結束時,最后再執行else后邊的動作。正常結束指循環不報錯或不碰到break。

for...else...結構

for 條件:
    動作
else:
    動作

 

 

三、while循環

只要條件符合,就可以一直執行動作。這種循環叫做 死循環,一經觸發,只要條件符合,就無線循環。

此條件實際上就是布爾值-->True、False。若想進行某判斷不定期結束循環,可設定變量為布爾值(True),達到目的想要結束時,重新設定布爾值(False)以結束循環。

循環在日常的使用中,還需要配合幾個表達方法,分別是:break、continue、end、else,在后文中會詳細講解。

while循環結構:

while 條件:
    動作
    

 打印只要滿足num小於等於10,就一直打印:

num = 1
while num <= 10:
    print(num)
    num = num + 1

只打印偶數:

num = 1
while num <= 100:
    if num % 2 == 0:                # $運算為取余數
        print(num)
    num = num + 1

只打印奇數:

num = 1
while num <= 10:
    print(num)
    num = num + 2

 

break的用法:

終止循環,當循環碰到break就會立即終止。

打印1到10的整數,當num==4的時候,終止循環。

num = 1
while num <= 10:
    print(num)
    num = num + 1
    if num ==4:
        break

 

continue的用法:

跳出次循環,之后的循環繼續,不受到影響。

此循環首先是加一次1再進行判斷打印,所以第一次打印出來的是'2',當'num=4'時,continue跳過或者說是結束了這次循環,不執行continue的結果,所以不打印'4',當最后一次循環的時候'num = 10',和第一次一樣,是先加1,再進行判斷打印,所以會出現'11'。

num = 1
while num <= 10:
    num = num + 1
    if num == 4:
        continue
    print(num)

 

else的用法:

當循環正常結束時,最后再執行else后邊的動作。正常結束指循環不報錯或不碰到break。

while...else...結構:

while 條件:
    動作
else:
    動作

else用法測試:

num = 1
while num <= 10:
    num = num + 1
    if num == 4:
        continue
    print(num)
else:
    print("This is else statement")l

 

四、表達式小練習

猜用戶年齡:

首先給定一個一個默認的用戶年齡,在input中輸入猜測的年齡,如果猜對了,就終止循環,如果錯了,就一直循環下去。

此條件實際上就是布爾值-->True、False。若想進行某判斷不定期結束循環,可設定變量為布爾值(True),達到目的想要結束時,重新設定布爾值(False)以結束循環。也可以直接給定布爾值(True),如果對了,使用break結束循環。

real_age = 50
flag = True

while flag:
    user_input_age = int(input("please input age:"))
    if user_input_age == real_age:
        print("Yes")
        flag = False
    elif user_input_age > real_age:
        print("you shoud input small!")
    else:
        print("you shoud input biger!")
print("End")
View Code

 

雙while嵌套測試:

  打印num1小於等於5時,num2后邊接0-7。使用end改變換行符。

num1 = 0

while num1 <= 5:
    print(num1,end="_")
    num1 = num1 + 1
    num2 = 0
    while num2 <= 7:
        print(num2,end="-")
        num2 = num2 + 1
    print()
View Code

 

輸出長方形:

  使用#號輸出一個長方形,用戶可以指定長和高,如果長為3,高為4,則輸出一個橫着有3個#豎着有4個#號的長方形。

long = int(input("Long:"))
height = int(input("Height:"))
num_height = 1

while num_height <= height:                                 # 當高小於出入的高時,while循環成立
    num_long = 1                                             # 定義默認長等於1
    while num_long <= long:                                 # 當長小於等於輸入的長時,while循環成立
        print("#", end="")                                  # 打印#號不換行,也就是說當輸入的長是幾就答應幾個#號
        num_long = num_long + 1                              # 循環完一次每次num_long+1,一次循環完成其實是打印一行
    print()                                                  # 換行
    num_height = num_height + 1                              # 控制高度,每次+1,實質上高度就是每次內循環結束后的強制換行
View Code

 

輸出直角三角形

  輸出一個直角三角形,用戶指定行數如下圖(如果上下翻轉,怎么實現?)

######
#####
####
###
##
#
line = int(input("Line:"))
while line > 0:
    tmp = line
    while tmp > 0:
        print("#", end="")
        tmp = tmp - 1
    print()
    line = line - 1
View Code

 

打印九九乘法表

second = 1

while second <= 9:
    first = 1
    while first <= second:
        print(str(first)+ "*"+ str(second)+ "="+ str(first * second), end="\t")
        first = first + 1
    print()
    second = second + 1
View Code

 

編寫登陸程序

  編寫一個登陸程序,要求用戶輸入賬號密碼,三次機會,如果三次機會沒有輸入正確,詢問用戶時候繼續要三次登陸機會,如果不需要,退出程序,如果需要,再給三次登陸機會。

_use = "Mu"
_passwd = "admin"
count = 0

while count < 3:
    username = input("Username: ")
    password = input("Password: ")
    count = count + 1
    if username == _use and password == _passwd:
        print("Welcome %s login..." % username)
        break
    else:
        print("Username or Password is Falst")
    if count == 3:
        yes_no = input("Do you want continue test? Please input Yes or No:")
        if yes_no == "Yes":
            count = 0
else:
    print("test login too many!!!")
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM