1.使用while循环输入 1 2 3 4 5 6 8 9 10
代码:
i = 0
while i < 10 :
i = i + 1
if i == 7:
continue
else:
print(i)
2.求1-100的所有数的和
代码:
i = 0
sum = 0
while i <100:
i = i + 1
sum = sum + i
3.输出 1-100 内的所有奇数
代码:
i = 0
while i < 101:
if i%2 !=0:
print(i)
i = i + 1
4.输出 1-100 内的所有偶数
代码:
i = 1
while i < 101:
if i%2 ==0:
print(i)
i = i + 1
5.求1-2+3-4+5 ... 99的所有数的和
代码:
i = 0
sum = 0
while i < 101:
if i%2 !=0:
sum = sum + i
elif i%2 ==0:
sum = sum - i
i = i + 1
print(sum)
6.用户登陆(三次机会重试)
代码:
count = 0
while count < 3:
name = input("请输入用户名:")
pwd = input("请输入密码:")
if name == "xiaoxing" and pwd == "123":
print("欢迎,%s"%(name))
break
else:
print("用户名或密码错误,请重新输入")
count = count + 1