小白的Python之路 day1 表达式if ... else ,while循环,for循环
表达式if ... else
一、用户登陆验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 提示输入用户名和密码 # 验证用户名和密码 # 如果错误,则输出用户名或密码错误 # 如果成功,则输出 欢迎,XXX! #!/usr/bin/env python # -*- coding: encoding -*- _username = 'qian'_password = 'zxc123'
username = input("username:")
password = input("password:")
if _username == username and _password == password:
print("Welcome user {name} login...".format(name=username))
else:
print("Invalid username or password!")
|
二、猜年龄游戏
在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了,
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/env python # -*- coding: utf-8 -*- my_age = 26
guess_age = int(input("guess age:") )
if guess_age == my_age :
print("yes, you got it. ")
elif guess_age > my_age:
print("think smaller...")
else:
print("think bigger!")
|
二、while loop
有一种循环叫死循环
1
2
3
4
5
|
count = 0 while True : print ( "死循环" ,count) count + = 1 |
上面猜年龄用while 循环去实现: 只猜3次
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/usr/bin/env python # -*- coding: utf-8 -*- count = 0
my_age = 26
while count < 3:
guess_age = int(input("guess age:") )
if guess_age == my_age :
print("yes, you got it. ")
break
elif guess_age > my_age:
print("think smaller...")
else:
print("think bigger!")
count +=1
else:
print("you have tried too many times..fuck off")
|
三、表达式for loop
最简单的循环5次
1
2
3
4
5
6
|
#_*_coding:utf-8_*_ __author__ = 'Many Qian' for i in range ( 5 ): print ( "loop:" , i ) |
输出:
1
2
3
4
5
|
loop: 0 loop: 1 loop: 2 loop: 3 loop: 4 |
需求一:还是上面的程序,但是遇到小于5的循环次数就不走了,直接跳入下一次循环
1
2
3
4
|
for i in range ( 10 ): if i< 5 : continue #不往下走了,直接进入下一次loop print ( "loop:" , i ) |
需求二:还是上面的程序,但是遇到大于5的循环次数就不走了,直接退出
1
2
3
4
|
for i in range ( 10 ): if i> 5 : break #不往下走了,直接跳出整个loop print ( "loop:" , i ) |
用上面学的for循环做猜年龄:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/usr/bin/env python # -*- coding: utf-8 -*- my_age = 26
for i in range(3):
guess_age = int(input("guess age:") )
if guess_age == my_age :
print("yes, you got it. ")
break
elif guess_age > my_age:
print("think smaller...")
else:
print("think bigger!")
else:
print("you have tried too many times..fuck off")
|
小白的Python之路 day1 表达式if ... else ,while循环,for循环的更多相关文章
- 小白的Python之路 day1
Python之路,Day1 - Python基础1 本节内容 Python介绍 发展史 Python 2 or 3? 一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van ...
- 小白的Python之路 day1 变量
Python之路,Day1 - Python基础1 变量 变量用于存储在计算机程序中引用和操作的信息.它们还提供了一种用描述性名称标记数据的方法,这样我们的程序就能更清晰地被读者和我们自己理解.将变量 ...
- 小白的Python之路 day1 用户输入
1 2 3 4 5 6 7 8 #!/usr/bin/env python #_*_coding:utf-8_*_ username =input("username:") p ...
- 小白的Python之路 day1 模块初识
模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持,以后的课程中会深入讲解常用到的各种库,现在,我们先来象征性的学2个简单的. ...
- 小白的Python之路 day1 Python3的bytes/str之别
原文:The bytes/str dichotomy in Python 3 Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分.文本总是Unicode,由str类型表示,二 ...
- 小白的Python之路 day1 数据类型,数据运算
一.数据类型初识 1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4.(-5+4j ...
- 小白的Python之路 day1 pyc
pyc是个什么? 1. Python是一门解释型语言? 我初学Python时,听到的关于Python的第一句话就是,Python是一门解释性语言,我就这样一直相信下去,直到发现了*.pyc文件的存在. ...
- 小白的Python之路 day1 字符编码
字符编码 python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill) ASCII(American Standard Code for Information Interc ...
- Python之路,Day1 - Python基础1
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
随机推荐
- Python字符编码详解(转)
http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html 在没有reload(sys)之前调用sys.setdefaultencoding ...
- linux下高可用LVS搭建及配置方法
一,安装与配置ipvsadm ipvsadm --help #查询是否安装成功 二,配置Director Server服务器 1. ifconifg eth0:0 183.61.87.4 ...
- jq获取浏览器的高度
// console.log("2-"+$(window).height()); //浏览器当前窗口可视区域高度 // console.log("3-"+$(d ...
- mysql 5.7.13 安装配置方法图文教程(linux) (转)
http://www.jb51.net/article/87160.htm ************************************************ linux环境Mysql ...
- 快速拥有各种数据访问SqlHelper
常加班食不按时,偶得清闲嘴溃疡. 美食一顿成泡汤,自此自认忙命人. 这就是此情此景的我,回来聊代码. 列举ADO.NET中的五个主要对象,并简单描述? 答:Connection连接对象,Command ...
- 02-线性结构3 Reversing Linked List
题目 Sample Input: 00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 ...
- Gulp livereload
平时使用yeoman作为前端部署工具,感觉到yeoman构建工具虽然方便,但是速度和大小总是不尽人意. 最近看到了gulp http://gulpjs.com/ 比较感兴趣随动手一试 gulp的安装以 ...
- 学习笔记:UITabBarController使用详解
一.手动创建UITabBarController 最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLa ...
- Elasticsearch java api 基本搜索部分详解
文档是结合几个博客整理出来的,内容大部分为转载内容.在使用过程中,对一些疑问点进行了整理与解析. Elasticsearch java api 基本搜索部分详解 ElasticSearch 常用的查询 ...
- 利用nginx实现负载均衡和动静分离
1.Nginx介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 . Nginx 是由 ...