1、使用while循环输入 1 2 3 4 5 6 8 9 10
count = 0
while count <= 9:
count += 1
if count == 7:continue
print(count)
或:
# 把7换成空格
count = 0
while count < 10:
count += 1
if count == 7:
print(' ')
else:
print(count)
# 不输入空格
count = 0
while count < 10:
count += 1
if count == 7:continue
print(count)
# 用pass
count = 0
while count <= 9:
count += 1
if count == 7:
pass #直接跳过
else:
print(count)
2、求1-100的所有数的和
count = 0
sum = 0
while count < 100:
count += 1
sum = sum + count
print(sum)
3、输出 1-100 内的所有奇数
count = 0
while count < 100:
count += 1
if count % 2 == 0:continue
print(count)
或:
# 第一种方法
count = 1
while count < 101:
print(count)
count += 2
# 第二种方法
count = 1
while count < 101:
if count % 2 == 1:
print(count)
count += 1
4、输出 1-100 内的所有偶数
count = 0
while count < 100:
count += 1
if count % 2 == 1:continue
print(count)
5、求1-2+3-4+5 ... 99的所有数的和
power = 1
count = 0
sum = 0
while count < 99:
count += 1
power += 1
sum = sum + count*((-1)**power)
print(sum)
或:
count = 1
sum = 0
while count < 100:
if count % 2 == 0:
sum -= count
else:
sum += count
count += 1
print(sum)
6、用户登陆(三次机会重试)
user_name = 'admin'
password = 'admin'
count = 0
while count < 3:
count += 1
name = input ('请输入用户名:')
if name == user_name:
pw = input('请输入密码:')
if pw == password:
print('登陆成功')
break
else :print('密码错误!')
if count == 3:print('登陆超过三次!请明天重试。')
else :
print('用户名不存在!')
if count == 3 :print('登陆超过三次!请明天重试。')
或:
i = 0
while i <3:
username = input('请输入账号:')
password = input('请输入密码:')
if username == 'admin' and password == 'admin':
print('登陆成功')
else:
print('登录失败,请重新登录')
i += 1