问题1、使用while循环输入 1 2 3 4 5 6 8 9 10
i = 0 while i<10: i+=1 if i ==7: continue print(i)
问题2 求1-100的所有数的和
i=0 sum = 0 while i <100: i +=1 sum = i +sum print(sum)
问题3 输出 1-100 内的所有奇数
i = 0 while i <100: i = i +1 num = i %2 if num ==1: print(i)
问题4 输出 1-100 内的所有偶数
i = 0 while i <100: i = i +1 num = i %2 if num ==0: print(i)
问题5 求1-2+3-4+5 ... 99的所有数的和
i = 0 sum = 0 while i <100: i = i+1 num = i%2 if num == 1: sum = sum+i else: sum = sum -i print(sum)
问题6 用户登陆(三次机会重试)
password = 123 conunt = 3 while conunt>0: conunt = conunt-1 shru = input('请输入密码') if shru == password: print('zhengque') elif conunt >0: print('输入不正确') print('你还有%s 次机会' % (conunt)) else: print('所有机会都用完了,退出程序')
升级版..
zhanhao = 'wang' password = 123 jishu = 3 while jishu>0: jishu = jishu -1 shuru1 = input('请输入账号') shuru2 = input('请输入密码') if shuru1 ==zhanhao and shuru2 ==password: print('输入正确') elif jishu >0: print('输入错误,你还有%s次机会'%(jishu)) else:print('3次机会用完了 程序退出')
年与时驰,意与日去,遂成枯落,
多不接世,悲守穷庐,将复何及。