欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python之路 - while,for循环小习题

程序员文章站 2022-07-14 23:35:34
...
#1、使用while循环输入 1 2 3 4 5 6     8 9 10
n = 1
while n < 11:
	if n == 7:
		pass
	else:
		print(n)
	n = n + 1
	

#2.求1-100的所有数的和
sum = 0
for i in range(1,101):
	sum += i
print (sum) 

#3.输出 1-100 内的所有偶数
for i in range(1,101):
	if i % 2 == 0:
		print (i)


#4输出 1-100 内的所有奇数
for i in range(1,101):
	if i % 3 == 0:
		print(i)   

#5.求1-2+3-4+5 ... 99的所有数的和
n = 1
s = 0
while n < 100:
	temp = n % 2
	if temp == 0:
		s = s - n
	else:
		s = s + n
	n = n + 1
print (s)
#------------------------------------------------
n = 0
for i in range(1,100):
	if i % 2 == 0:
		n -= i
	else:
		n += i
print(n)

#6。用户登陆(三次机会重试)"""

num = 0
while True:
    user = input("UserName:")
    pwd = input("PassWord:")
    if user == "root" and pwd == "123":
        print ("登录成功...")
        break
    elif num > 2:
        print ("用户名或密码错误...")
        print ("输入次数达到上限,退出程序...")
        break
    else:
        print ("用户名或密码错误,请重新输入...")
        num += 1
        continue