1. 使用while循环输出1 2 3 4 5 6 8 9 10
count=1 while count <= 10: if count == 7: count+=1 continue print(count) count+=1
2. 求1-100的所有数的和
n=0 count=1 while count <= 100: res+=count count+=1 print(n)
3. 输出 1-100 内的所有奇数
count=0
while count <= 100:
if count%2 == 1:
print(count)
count+=1
4. 输出 1-100 内的所有偶数
count=1 while count <= 100: if count%2 == 0: print(count) count+=1
5. 求1-2+3-4+5 ... 99的所有数的和
res=0 count=1 while count <= 5: if count%2 == 0: res-=count else: res+=count count+=1
print(res)
6. 用户登陆(三次机会重试)
while count < 3: name=input('请输入用户名:') password=input('请输入密码:') if name == 'egon' and password == '123': print('login success') break else: print('用户名或者密码错误') count+=1
7:猜年龄游戏
age=18
count=0
while count < 3:
n=int(input('>>: '))
if n == age:
print('恭喜你答对了')
break
count+=1
8:猜年龄游戏升级版
age=18
count=0
while True:
if count == 3:
h=input('继续(Y/N?)>>')
if h == 'Y' or h == 'y':
count=0
else:
break
n=int(input('>>: '))
if n == age:
print('恭喜你答对了')
break
count+=1