python 练习题

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

count = 1
while count < 11:
	if count == 7:
		pass
	else:
		print(count)
	count = count + 1
print('----end----')

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

1 count = 1
2 while count < 101:
3     temp = count % 2
4     if temp == 0:
5         pass
6     else:
7         print(count)
8     count = count + 1
9 print('----end----')

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

1 count = 1
2 while count < 101:
3     temp = count % 2
4     if temp == 0:
5         print(count)
6     else:
7         pass
8     count = count + 1
9 print('----end----')

4.求1-100的所有数的和,并打印相加的值的变化过程

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

 5.求1-2+3-4+5 ... 99的所有数的和

 

n = 1
s = 0 # s是之前所有数的总和
while n < 100:
	temp = n % 2
	if temp == 0:
		s = s - n	
	else:
		s = s + n						
	n = n + 1	
print(s)

6.while循环   之 continue  、 break

count = 0
while count < 10:
	count = count + 1
	continue # 终止当前循环,开始下一次循环
	print(222222)
print('-----end------')

  

count = 0
while count < 10:
	count = count + 1
	print(count)
	break  #终止整个循环
	print(222222)
print('-----end------')

 7.用户登陆(三次机会重试)

count = 0
while count < 3:
    user = input("请输入用户名:")
    pwd = input("请输入密码:")
    if user =="root" and pwd == "root!123":
        print("欢迎登陆")
        break
    else:
        print("用户名或密码错误")    
    count = count + 1
else:
    print("您的输入次数已达上限,请等待10分钟之后再试")#目前还未学习如何读秒,给程序加等待时间以及提示信息

 

posted on 2019-03-11 18:13  测试小伙子  阅读(78)  评论(0编辑  收藏  举报

导航