1.使用while循环输出1,2,3,4,5,6, 8,9,10
#!/usr/bin/python3 n = 1
while n <= 10:
if n != 7:
print(n)
else:
pass
n += 1
2.求1-100的所有数之和
#!/usr/bin/python3
n = 1
sum = 0
while n <= 100:
sum += n
n += 1
print(sum)
3.输出1—100内所有的奇数
#!/usr/bin/python3 n = 1
while n <= 100:
if n % 2 != 0:
print(n)
else:
pass
n += 1
4.输出1—100内所有的偶数
#!/usr/bin/python3 n = 1
while n <= 100:
if n % 2 == 0:
print(n)
else:
pass
n += 1
5.求1-2+3-4+5…99的所有数的和
#!/usr/bin/python3 n = 1
sum = 0
while n< 100:
if n % 2 != 0:
sum += n
else:
sum -= n
n += 1
print(sum)
6.用户登录,三次重试机会
#!/usr/bin/python3 user_name = "admin"
pwd = "123456"
num = 1 while True:
n1 = input("用户名:")
n2 = input("密码:")
if n1 == user_name and n2 == pwd:
print("登陆成功!")
break
else:
num += 1
if num == 4:
print("3次用户名或者密码均有误!退出程序.")
break