其他分享
首页 > 其他分享> > 流程控制之while(2)

流程控制之while(2)

作者:互联网

1. 使用while循环输出1 2 3 4 5 6     8 10
count = 0
while count < 10:
count += 1
if count == 7 or count == 9:
continue
print(count)
2.求1-100的所有数的和
num = 0
count = 1
while count <= 100:
num = num+ count #num+=count
count += 1
print(num)
3. 输出 1-100 内的所有奇数
count = 0
while count <100:
count+=1
if count%2==1:
print(count)
4. 输出 1-100 内的所有偶数
count = 0
while count <100:
count+=1
if count%2==0:
print(count)
5. 求1-2+3-4+5 ... 99的所有数的和
num = 0
count = 1
while count <= 100:
if count%2==0:
num = num-count #num-=count
elif count%2==1:
num = num+ count #num+=count
count += 1
print(num)
6.用户登陆(三次机会重试)
name = "sean"
pwd ="11"
count = 0
while count <3:
inp_name = input('your name:')
inp_pwd =input('your pwd:')
if inp_name == name and inp_pwd == pwd:
print('success')
break
else:
print('try again')
count +=1
else:
print('账号已锁定,请30分钟后再次尝试')
7:猜年龄游戏:允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
age_of_sean='18'
count=0
while count < 3:
age = input('age_of_sean is:')
if age == age_of_sean:
print("It's good")
break
else:
print('Try again')
count+=1
else:
print('本轮游戏结束,下次加油!')
8:猜年龄游戏升级版
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出
age_of_sean='18'
count=0
while True:
if count == 3:
choice=input('如果继续,请输入Y或y,否则输入N或n退出: ')
if choice == 'Y' or choice == 'y': #区分大小写
count=0
else:
print('游戏结束,下次继续加油哦')
break
age=input('age_of_sean is: ')
if age == age_of_sean:
print("It's good")
break
count+=1

标签:count,控制,0while,流程,while,num,sean,print,age
来源: https://www.cnblogs.com/datatool/p/13341435.html