重温python基础-while循环
使用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)
使用while循环打印0-100的数字:
-----------打印0-100的数字---------------- count = 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)
使用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 + 1
使用while循环打印1-100内的奇数:
# ----------打印1-100之间的奇数--------------- # count = 1 # while count <= 100: # if count%2 == 1: # print(count) # count = count + 1