I. Using while loop output 1 2 3 4 5 6 8 9 10
1 #!/USR/BIN/EVC Python 32 #-*-coding:utf-8-*-3 4 " "5 using while loop Output 1 2 3 4 5 6 8 96 " "7 8Count = 19 whilecount<=10:Ten ifCount = = 7: One Pass A Else: - Print(count) -Count + = 1
Second, using the while loop output 1~100 and
1 #!/USR/BIN/EVC Python 32 #-*-coding:utf-8-*-3 4 " "5 using the while loop to output 1~100 and6 " "7 8i = 19sum =0Ten whilei<=100: Onesum = sum +I Ai + = 1 - Print(sum)
Three, using the while loop output 1~100 all the odd
# !/USR/BIN/EVC Python 3 # -*-coding:utf-8-*- " " use the while loop to output all the odd numbers within the 1~100 " " = 0 while I <: i+=1 while i% 2! = 0: Print (i) + = 1
Iv. using the while loop to output all the even numbers within the 1~100
# !/USR/BIN/EVC Python 3 # -*-coding:utf-8-*- " " using the while loop to output all the even numbers within the 1~100 " " = 0 while I <: i+=1 while I% 2 = = 0: Print (i) + = 1
V. Using the while loop output 1-2+3-4+5.....+99 and
1 #!/USR/BIN/EVC Python 32 #-*-coding:utf-8-*-3 4 " "5 using the while loop to output 1-2+3-4+5.....+99 and6 " "7 8i = 19sum =0Ten whilei<=99: One ifI% 2 = =0: Asum = Sum-I - Else: -sum = sum +I thei + = 1 - Print(sum)
Six, determine the user login, three chance to retry
1 #!/USR/BIN/EVC Python 32 #-*-coding:utf-8-*-3 4 " "5 User Login, three chance retry6 " "7 8 9Count = 1Ten whileCount <= 3: One A ifInput'Please enter user name:') =='liming': - Print('Login Successful! ') - Break the Else: - Print('Please try again! ') -Count + = 1 - Else: + Print('has failed three times and cannot log in today. ')
Python Foundation-----while loop exercise