码迷,mamicode.com
首页 > 编程语言 > 详细

Python练习题

时间:2018-02-28 17:32:59      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:bre   class   成功   输入   sum   put   test   登陆   htm   

 

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

i=0
while i<10:
	i=i+1
	if i==7:
		continue
	print(i)

  结果:

E:\python\python>python test.py
1
2
3
4
5
6
8
9
10

2、求1-100的所有数的和

i=0
sum=0
while i<100:
	i+=1
	sum =sum+i 
print(sum)

  结果:

E:\python\python>python test.py
5050

3、输出 1-100 内的所有奇数

i=0
while i<100:
	i+=1
	if i%2==1:
		print(i)

  结果:

E:\python\python>python test.py
1
3
5
7
......
99

4、输出 1-100 内的所有偶数

i=0
while i<100:
	i+=1
	if i%2==0:
		print(i)

  结果:

E:\python\python>python test.py
2
4
6
......
100

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

i=0
sum=0
while i<99:
	i+=1
	if i%2==0:
		sum=sum-i
	if i%2==1:
		sum=sum+i
print(sum)

  结果:

E:\python\python>python test.py
50

6、用户登陆(三次机会重试)

sum=0
while sum<3:
	sum+=1
	username=input("请输入你的名字:")
	password=input("请输入你的密码:")
	if username==‘ma‘ and password==‘123456‘:
		print("您已经成功登录")
		break
	else:
		print("您输入的用户或密码有误")	
		print("---------------------------")
		print("")
		continue

  结果:

E:\python\python>python test.py
请输入你的名字:ls
请输入你的密码:ls
您输入的用户或密码有误
---------------------------

请输入你的名字:ls
请输入你的密码:ls
您输入的用户或密码有误
---------------------------

请输入你的名字:ls
请输入你的密码:ls
您输入的用户或密码有误
---------------------------


E:\python\python>python test.py
请输入你的名字:ls
请输入你的密码:ls
您输入的用户或密码有误
---------------------------

请输入你的名字:ma
请输入你的密码:123456
您已经成功登录

E:\python\python>

 

Python练习题

标签:bre   class   成功   输入   sum   put   test   登陆   htm   

原文地址:https://www.cnblogs.com/machangwei-8/p/8484460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!