练习题:
1.使用while循环输入1 2 3 4 5 6 8 9 10
a = 0
while a < 10 :
a = a + 1
if a == 7 :
pass
else :
print(a)
2.求1-100的所有数的和
a = 1
s = 0
while a < 101 :
s = s + a
a = a + 1
print(s)
3.输出1-100内的所有奇数
a = 0
while a < 100 :
a = a + 1
b = a % 2
if b == 1 :
print(a)
else :
pass
4.输出1-100内的所有偶数
a = 0
while a < 100 :
a = a + 1
b = a % 2
if b == 0 :
print(a)
else :
pass
5.求1-2+3-4+5...99的所有数的和
a = 1
s = 1
while a < 100 :
a = a + 1
if a % 2 == 0 :
s = s - a
else :
s = s + a
print(s)
6.用户登陆(三次计划重试)
a = 0
while a < 3 :
name = input("用户名:") ;
pwd = input("密码:") ;
if name == ('root') :
if pwd == ('123') :
print('欢迎登陆系统')
break
else :
a = a + 1
print('账号或密码错误')
else :
a = a + 1
print('账号或密码错误')
第二种方法:
count = 0 while count < 3: user = input('username:') pwd = input('password:') if user == 'root' and pwd == '123': print('欢迎登陆') print('..........') break else: count = count + 1 print('用户名或者密码错误:',count,"次")