1、简单的if语句
if语法
if 要判断的条件:
条件成立的时候,要做的事
else:
条件不成立时,要做的事
示例
""
# _*_coding:utf-8 _*_
Name:python-test-01.py
Date:3/18/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
age = 20 #定义一个变量
#判断是否满足条件
if age >= 18:
print("你已成年,欢迎!!")
else:
print("sorry!!")
[kiosk@foundation60 test]$ python3.6 python-test-01.py 运行结果
你已成年,欢迎!!
2、if 逻辑运算
- and
条件1 and 条件2
两个条件同时满足,就返回True
两个条件有一个不满足,就返回False
"""
# _*_coding:utf-8 _*_
Name:if-test_02.py
Date:3/28/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
age = int(input("请输入年龄: "))
if age >= 0 and age < 100:
print("正确")
else:
print("错误")
[kiosk@foundation60 test]$ python3.6 if-test_02.py ##运行结果
请输入年龄: 45
正确
- or:
条件1 or 条件2
两个条件只要有一个满足,就返回True
两个条件都不满足,返回False
"""
# _*_coding:utf-8 _*_
Name:if-test_02.py
Date:3/28/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
py_score = int(input('请输入python成绩: '))
linux_score = int(input('请输入linux成绩: '))
if py_score >= 60 or linux_score >= 60 :
print('成绩合格,可以毕业!')
else:
print('您需要延毕!!')
[kiosk@foundation60 test]$ python3.6 if-test_02.py
请输入python成绩: 45
请输入linux成绩: 78
成绩合格,可以毕业!
3、if_elif
- 语法结构
if 要判断的条件:
条件成立时,要做的事
elif 条件2:
...
else:
条件都不成立时,要做的事
elif 和 else都必须和if联合使用,不能单独使用
- 示例
"""
# _*_coding:utf-8 _*_
Name:if_elif.py
Date:3/28/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
holiday_name=input('请输入节假日名称: ')
if holiday_name == '春节':
print('吃饺子,放鞭炮!')
elif holiday_name == '元宵节':
print('吃元宵,赏灯!')
elif holiday_name == '中秋节':
print('吃月饼,赏月')
else:
print('上班,上学,不过节!')
[kiosk@foundation60 test]$ python3.6 if_elif.py #运行结果
请输入节假日名称: 中秋节
吃月饼,赏月
4、if嵌套
- 语法结构
if 条件1:
条件1满足执行的动作
if 满足条件1的基础上的条件2:
...
else:
条件2不满足的情况下
else:
条件1不满足时,执行的动作
- 示例:有车票并且携带刀具短于20cm即可过安检
"""
# _*_coding:utf-8 _*_
Name:if_else嵌套循环.py
Date:3/28/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
ticket = input('请出示您的车票: ')
if ticket == '有':
print('请安检')
knife_len = int(input('道具长度: '))
if knife_len <= 20:
print('欢迎乘车!!')
else:
print('你携带刀具超过20cm,不能乘车!')
else:
print('请您先购买车票!')
[kiosk@foundation60 test]$ python3.6 if_else嵌套循环.py ##运行结果
请出示您的车票: 有
请安检
道具长度: 23
你携带刀具超过20cm,不能乘车!
5、练习
- 例1:判断闰年?->用户输入年份year, 判断是否为闰年?
"""
# _*_coding:utf-8 _*_
Name:if_test_01.py
Date:3/28/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
year = int(input('请输入: '))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print('%d年是闰年' %(year))
else:
print('%d年不是闰年' %(year))
[kiosk@foundation60 test]$ python3.6 if_test_01.py
请输入: 2014
2014年不是闰年
- 示例2:输入年、月,输出本月有多少天。
"""
# _*_coding:utf-8 _*_
Name:if_test_02.py
Date:3/28/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
"""
输入样例1:2004 2
输出结果1:本月29天
输入样例2:2010 4
输出结果2:本月30天
"""
Year = int(input('请输入年份:'))
Month = int(input('请输入月份:'))
a = Year % 4
b = Year % 100
c = Year % 400
if Month == 2:
if (a == 0 and b != 0) or (c == 0):
print('本月是29天')
else:
print('本月是28天')
elif Month == 1 or Month == 3 or Month == 5 or Month == 7 or Month == 8 or Month == 10 or Month == 12:
print('本月是31天')
else:
print('本月是30天')
[kiosk@foundation60 test]$ python3.6 if_test_02.py
请输入年份:2016
请输入月份:2
本月是29天
- 示例3
"""
# _*_coding:utf-8 _*_
Name:if_test_03.py
Date:3/28/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
"""
1.从控制台输入要出的拳 --石头1 剪刀2 布3
2.电脑随机出拳
3.比较胜负
"""
import random
print('你要可以作出的选择是:1.石头;2.剪刀;3.布') ##从控制台输入要出的拳
Player=int(input('你的选择是:'))
Computer=random.randint(1,3) ##电脑随机出拳
print('电脑:', Computer)
if (Player == 1 and Computer == 3) or(Player == 2 and Computer == 1)or(Player == 3 and Computer == 2):
print('电脑玩家胜利')
elif (Player == 1 and Computer == 2) or (Player == 2 and Computer == 3) or (Player == 3 and Computer == 1):
print('You win')
else:
print('平局')
[kiosk@foundation60 test]$ python3.6 if_test_03.py
你要可以作出的选择是:1.石头;2.剪刀;3.布
你的选择是:3
电脑: 3
平局
[kiosk@foundation60 test]$ python3.6 if_test_03.py
你要可以作出的选择是:1.石头;2.剪刀;3.布
你的选择是:2
电脑: 1
电脑玩家胜利