while练习题

# 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的所有数的和
res=0
count = 1
while count<=100:
res += count #res=res+count
count+=1 #count=count+1
print(res)
 # 3. 输出 1-100 内的所有奇数
count = 1
while count <= 100:
if count % 2 != 0:
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 <100:
if count % 2 == 0:
res -= count
else:
res += count
  count+=1
print(res)
# 6. 用户登陆(三次机会重试)
print('start')
count=0
while count<=2:
name=input('your name: ')
pwd=input('your password;')
if name=='summer'and pwd=='12345':
print('success')
break
else:
print('user or password err')
count+=1
else:
print('end')
#7:猜年龄游戏
要求:
    允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
age = 18
count = 0
while count <= 2:
guess = int(input('age:'))
if guess == age:
print('恭喜')
break
else:
print('err')
count += 1
else:
print('退出')
#8:猜年龄游戏升级版 
要求:
    允许用户最多尝试3次
    每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
    如何猜对了,就直接退出 
age = 18
count = 0
while True:
if count == 3:
choice = input('继续Y/N:')
if choice == 'Y' or choice == 'y':
count = 0
else:
break
guess = int(input('age:'))
if guess == age:
print('success')
break
count += 1
 
 

猜你喜欢

转载自www.cnblogs.com/xiamenghan/p/9656346.html