重溫python基礎-跟隨Alex學習while循環
阿新 • • 發佈:2019-03-03
spl lex 其中 == 1-1000 style div 代碼 else
View Code
使用while循環打印1-1000內的數字代碼如下:
#打印1-1000內的數字 count = 1 while True: print(count) count +=1 #count = count + 1 if count == 1001: break #打印1-1000內的數字 count = 0 while count <= 999: count = count + 1 print(count)View Code
使用while循環打印0-100的數字:
-----------打印0-100的數字---------------- countView Code= 0 while count <= 100: print(count) count = count + 1 -----------打印0-100的數字---------------- count = 0 while count <= 99: count = count + 1 print(count)
使用while循環打印1-100之間的偶數:
-------------打印1-100之間的偶數----------- count = 0 while count <= 98: if count%2 ==0: count= count + 2 print(count) --------------打印1-100之間的偶數------------ count = 1 while count <= 100: if count%2==0: print(count) count = count + 1 ----------打印1-100之間的奇數--------------- count = 1 while count <= 100: if count%2 == 1: print(count) count = count + 1
使用for循環打印1-100之間的偶數:
打印1-100的偶數,使用for循環實現 for i in range(1,101): if i%2 == 0: print(i)View Code
使用while循環配合if-else語句打印1-100之間的數字,其中50不打印,60-80之間的數字打印對應值的平法:
# -------打印1-100.第50次不打印,第60-80次打印對應值的平法 count = 1 while count <= 100: if count == 50: pass if count >= 1 and count <= 49: print(count) if (count >= 60) and (count <= 80): print(count*count) count = count + 1View Code
使用while循環打印1-100內的奇數:
# ----------打印1-100之間的奇數--------------- # count = 1 # while count <= 100: # if count%2 == 1: # print(count) # count = count + 1View Code
重溫python基礎-跟隨Alex學習while循環