流程控制之while循环----作业
- 使用while循环输出1 2 3 4 5 6 8 9 10
count = 1
while count<11:
if count == 7:
count+=1
continue
print(count)
count+=1
- 求1-100的所有数的和
count = 0
sum = 0
while count < 100:
count += 1
sum = sum+ count
print(sum)
```python
- 输出 1-100 内的所有偶数
方法一:
n = 0
m = 0
while n < 100:
n+=1
if n == 2*m+1:
m+=1
continue
print(n)
方法二、
n= 0
while n < 100:
n+=1
if n%2 != 0:
continue
print(n)
- 输出 1-100 内的所有奇数
n = 1
while n < 101:
if n % 2 != 0:
print(n)
n += 1
- 用户登陆(三次机会重试)
count = 1
username = 'amanda'
password = '123'
while count < 4:
name = input('请输入用户名:')
pwd = input('请输入密码:')
if name == username and pwd == password:
print('输入成功')
else:
print('用户名或密码有错')
count+=1
print('你的机会已用完')
6:猜年龄游戏
要求:
#允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
age = '18'
n = 1
while n < 4:
guess_age = input('猜一猜年龄:')
if guess_age == age:
print('恭喜你,猜对了')
break
else:
print('请再猜一次')
n+=1
print('你的机会已经用完')
#7:猜年龄游戏升级版
#要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出
age = '18'
n = 0
while n <= 3:
guess_age = input('猜一猜年龄:')
if guess_age == age:
print('恭喜你,猜对了')
break
else:
print('请再猜一次')
n += 1
if n == 3:
answer = input('请问你是否要继续猜,请确定y/n:')
if answer == 'Y' or answer == 'y':
n = 0
elif answer == 'N' or answer == 'n':
break
print('游戏结束')
- 求1-2+3-4+5 … 99的所有数的和
n = 1
sum = 0
while n < 100:
if n %2 == 0:
sum = sum -n
else:
sum = sum + n
n+=1
print(sum)