一个例子使用条件和循环
2017-03-30 16:25
363 查看
一个例子结合使用while和for、if和else、break和continue,看看他们之间的联系和区别:
上面的代码还可以使用while-else语句实现,如下面:
(ps:但是我们一般不这样写代码,也不推荐这样写代码,while-else和for-else语句的写法不够清晰,对于不是Python程序员或者Python新手来说不够直观且容易让人误解,而Python语言不就是应该简单直接吗。)
def func(): valid=False count=3 while count>0: input1=input('Enter password:') for each in passwordlist: if input1==each: valid=True break if not valid: print("Erorr Input!") count-=1 if count == 0: return print('invalid input more than 3') continue else: return print('pass success!') >>> passwordlist=['123456','123','12','1234'] >>> func() Enter password:1 Erorr Input! Enter password:2 Erorr Input! Enter password:3 Erorr Input! invalid input more than 3 >>> func() Enter password:123 pass success!
上面的代码还可以使用while-else语句实现,如下面:
(ps:但是我们一般不这样写代码,也不推荐这样写代码,while-else和for-else语句的写法不够清晰,对于不是Python程序员或者Python新手来说不够直观且容易让人误解,而Python语言不就是应该简单直接吗。)
def func(): valid=False count=3 while count>0: input1=input('Enter password:') for each in passwordlist: if input1==each: valid=True break if not valid: print("Erorr Input!") count-=1 continue else: return print('pass success!') else: return print('invalid input more than 3') #retrun虽然可以不用,但是毕竟是个函数有个return更友好
相关文章推荐
- 一个例子解读$#,gt,expr,shift及循环的使用。
- 练习2-2 在不使用运算符&&或者||的条件下编写一个与上面的for循环语句等价的循环语句。
- 2.2 在不使用运算符&&或|| 的条件下编写一个与上面的for循环语句等价的循环语句。
- 练习 2-2 在不使用运算符&&或||的条件下编写一个与上面的for 循环语句等价的循环语句。
- 关于使用while循环时,括号内用自加自减作条件的一个疑问
- 2.2 在不使用运算符&&或|| 的条件下编写一个与上面的for循环语句等价的循环语句。
- 练习2-2 在不使用运算符&&或|| 的条件下编写一个与上面的for循环语句等价的循环语句
- 练习 2-2 在不使用运算符&&或||的条件下编写一个与上面的for 循环语句等价的循环语句。
- 收藏——在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- ado多次按条件使用一个记录集中的数据的方法
- 在MFC下使用OpenGL的一个简单的例子
- 使用JSP + JAVABEAN + XML 开发的一个例子
- 一个DirectX使用的例子
- 使用Hibernate的一个完整例子
- 使用Hibernate的一个完整例子
- 一个使用HashMap的Struts例子~~
- 一个使用自定义命名空间的Schema文件,xml文件和castor生成的java代码的例子
- 一个使用spring的最简单的例子。
- 使用Hibernate的一个完整例子
- 使用Hibernate来实现持久对象 (Hibernate的一个例子)