一、使用while循环输入 1 2 3 4 5 6 7 8 9 10 count = 1 while count < 11: if count == 7: count += 1 continue print(count) count +=1 else:print("end") 二、求1-100的所有数的和 count = 1 sum = 0 while count < 101: sum = sum + count count += 1 print(sum) 三、输出1-100内的所有奇数 count = 1 while count < 101: if count %2 == 1: print(count) count += 1 print("end") 四、输出1-100内所有偶数 count = 1 while count < 101: if count %2 ==0: print(count) count += 1 print("end") 五、求1-2+3-4+5....99的所有数的和 #方法一 sum = 0 count = 1 while count < 100: if count %2 == 0: sum = sum - count print(sum) elif count %2 == 1: sum = count + sum print(sum) count += 1 print(sum) 方法二 sum1 = 0 sum2 = 0 count = 1 while count < 100: if count %2 == 1: sum1 = sum1 + count else: sum2 = sum2 + count count += 1 print(sum1 - sum2) #六、用户登陆(三次机会重试) count = 1 while count < 4: name = input("please input your name=") password = input("please input your password=") if name == "xue" and password == "123": print("login successful") break else: print("login failed") count += 1 print("end")
一、用户交互.input
ps:python3 input
python2 raw_input
二、if语句.
三、while.
while 条件:
结果
结束循环:
1,改变条件.
2,break
break continue
break:在循环当中,遇到break,直接退出
continue:结束本次循环,继续下一次循环.