我是靠谱客的博主 安静大树,这篇文章主要介绍for循环与if配合使用的简单例子,现在分享给大家,希望可以做个参考。

先上代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python import getpass flag = True for i in range(3): user = input("Please input your username:") #采用getpass在接收输入的时候不会显示密码,but somehow getpass在pycharm里使用会出问题,需要在cmd里运行 passwd = getpass.getpass('Please input your password:') print(type(passwd)) passwd = int(passwd) print(type(passwd)) if user == 'Richard'and passwd == 123: print('{0} login successfully.'.format(user)) flag = False break else: print("Invalid username or password.") if flag: print("Wrong input more than 3 times. Program exit.")

运行结果:

复制代码
1
2
3
4
5
6
E:python 14>python day0102.py Please input your username:Richard Please input your password: <class 'str'> <class 'int'> Richard login successfully.

 另外顺便在这里提一下range的用法:

复制代码
1
2
3
4
5
for i in range(10): print(i) print('---------------------------') for i in range(0,10,2): print(i)

运行结果:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
D:PythonPython36python.exe "E:/python 14/day0102.py" 0 1 2 3 4 5 6 7 8 9 --------------------------- 0 2 4 6 8

 

转载于:https://www.cnblogs.com/Richard-Liang/p/10743880.html

最后

以上就是安静大树最近收集整理的关于for循环与if配合使用的简单例子的全部内容,更多相关for循环与if配合使用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部