Python学习【第2篇】:循环

For循环

pass

 

 

while 循环

pass

 

 

练习题:

1.使用while循环输入 1 2 3 4 5 6     8 9 10,不输出7

n = 1
while n< 11:
if n == 7:
pass
else:
print(n)
n = n + 1
print ("运行结束")
注意:代码块的缩进

2.输出1-100内所有奇数

n = 1
while n < 101:
if n % 2 == 0:
pass
else:
print(n)
n = n + 1
print("运行结束")

3.输出1-100内所有偶数

n = 1
while n < 101:
if n % 2 == 0:
print(n)
else:
pass
n = n + 1print("运行结束")

4.求1-100内数相加的和

n = 1
s = 0
while n < 101:
s = s + n
n = n + 1
print(s)

5.求1-2+3-4+5-6+.........99数之和

n = 1
s = 0
while n < 99:
temp = n % 2
if temp == 0:
s = s - n
else:
s = s + n
n = n + 1
print(s)
6、用户登陆(三次机会重试)

posted on 2018-05-05 16:23  小星1995  阅读(180)  评论(0编辑  收藏  举报

导航