Python练习代码
Posted yuan-g
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python练习代码相关的知识,希望对你有一定的参考价值。
1-100所有数字的和
count = 1 sum = 0 while count <= 100: sum = sum + count count += 1 print(sum)
输出1-100内的所有奇数
count = 1 num = 0 while count <= 100: count += 1 num += 1 if num%2 == 0: continue else: print(num)
输出1-100内所有的偶数
count = 1 num = 0 while count <= 100: count += 1 num += 1 if num%2 == 0: print(num)
求1-2+3-4+5...99的和
count = 1 num = 1 sum = 0 while count <= 99: if num%2 == 0: num = -num #判断奇数偶数,奇数为正,偶数为负 else: num = num sum = sum + num count += 1 num += 1 print(sum)
使用while循环输入1 2 3 4 5 6 8 9 10
count = 1 num = 0 while count < 10: count += 1 num += 1 if num == 7:continue else:print(input(‘请输入数字‘+str(num)))
用户登录(三次机会重试)
count = 1 while count <= 3: ID = input(‘请输入账号:‘) Password = input(‘请输入密码:‘) count += 1 if ID == ‘关元最帅‘ and Password == ‘无敌帅‘: print(‘登陆成功‘) break elif count <= 3: print(‘登陆失败,还有‘ + str(4-count) + ‘次机会‘) else: print(‘登陆失败超过三次‘)
以上是关于Python练习代码的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段