while迴圈計算規則:內迴圈—外迴圈!,while規則

來源:互聯網
上載者:User

while迴圈計算規則:內迴圈—外迴圈!,while規則

num= 1 #值 =1
while num <= 10 : # num(1)小於10
print(num) # 應該列印 這個1的值
num +=1 # num+=1等價於 num再加1 所以這個值一共是2
if num == 6: #如果為6 這個程式終止,如果沒有到6,那麼程式將會一遍一遍的走直到走到6
break
例如:
while num <= 10 : 這句話num的值是1,那麼print則會判定列印 1
但是在執行到了mum+ =1的地方,num這個值就被往上加了一位 變成了2
但是依舊沒有達到終止程式的條件,所以還要走第二輪。

已知第一輪最後num的值為2,那麼在此到達num+=1的地方 則會變成3,但是依舊沒有達到終止條件,
這樣就會繼續吧 4、 5都按照以上方法輸出一遍,到6自動停止。
所以最後得出的結果應該是 12345

 

while 變數判定
age = 18 #設變數為18
sign = True # sige變數等於(真)可判定條件
while sign : #while sign:定義
guess =int(input("guess_age:")) # guess = 使用者互動器所填寫的資料
if guess == age: #如果 guess變數等於 age變數 那麼
print("Yes Good!") #則判定 列印 yes
sign = False #如果 sign 等於 (假)不可執行條件
elif guess < age : #如若 guess 小於 age的值 那麼
print("you need to enter one bingger!")#則判定列印 you need to neter one bigger
else: #否則
print("you need to enter one smaller!") #則列印you need to enter one smaller!
print("end!") # 結束!

 


break 終止判定

age = 18
while True :#當判定(真)正確時
guess =int(input("guess_age:"))
if guess == age:#如果guess值等於 age那麼
print("Yes Good!")#判定為 yes good!
break #並終止!
elif guess < age : #否則如果 guess小於age
print("you need to enter one bingger!")
else:
print("you need to enter one smaller!")
print("end!")

while迴圈計算規則:

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.