标签:oob int str hid 代码 alt -- bre 账号
实例1:使用while循环输入 1 2 3 4 5 6 8 9 10
count = 0 while count < 10: count += 1 # count = count + 1 if count == 7: print(‘ ‘) else: print(count)
count = 0 while count < 10: count += 1 # count = count + 1 if count == 7: continue print(count)
实例2:输出 1-100 内的所有奇数
count = 1 while count < 101: print(count) count += 2
count = 1 while count < 101: if count % 2 == 1: print(count) count += 1
实例3:求1-2+3-4+5 ... 99的所有数的和
sum = 0 count = 1 while count < 100: if count % 2 == 0: sum = sum - count else: sum = sum + count count += 1 print(sum)
实例4:?户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使?字符串格式化)
username = "yangxiaoer" password = "123456" i = 3 while i > 0: zh = input("请输入你的账号:") i -= 1 if zh == username: mm = input("请输入你的密码:") if mm == password: print("验证成功.正在登陆......") print(‘‘‘恭喜你登陆成功! 欢迎用户进入 用户名 :%s 密码 :%s ‘‘‘%(zh,mm)) break else: if i == 0: print("你的机会已经没了!game over 下次见!") answer = input(‘再试试?Y or N‘) if answer == ‘Y‘: i = 3 print("密码错误,请重新输入") print("你还有"+str(i)+"次机会") else: print("请输入正确的用户名!") if i == 0: print("你的机会已经没了!") answer = input(‘再试试?Y or N‘) if answer == ‘Y‘: i = 3 print("你还有" + str(i) + "次机会") else: print(‘你TM要不要脸‘)
while else使用案例
count = 0 while count <= 5 : count += 1 if count == 3:break print("Loop",count) else: print("循环正常执行完啦") print("-----out of while loop ------")
参考参鸟教程:https://www.runoob.com/python/python-while-loop.html
标签:oob int str hid 代码 alt -- bre 账号
原文地址:https://www.cnblogs.com/qi1113/p/12349587.html