Python—if语句

一.格式

if   要判断的条件1:

      条件1成立的时候要做的事情

      。。。。

elif  要判断的条件2:
      条件2成立的时候要做的事情
      。。。。

else:

     条件不成立时候要做的事情



!!!elif与else不能单独使用,必须联合if才能使用

二.练习

#练习1

if语句

"""
# _*_ coding:utf-8 _*_
Name:09_if01.py
Date:19-1-12
Author:westos-dz
Connect:
Desc:
"""
"""
if 要判断的条件:
    条件成立的时候,要做的事
    ...
"""
#1.定义一个整数变量
# age = 18
age = 12
#2.判断是否满18岁
if age >= 18:
    print('欢迎来酒吧')

print('~~~~~~~~~~')

#练习2

if-else语句

"""
# _*_ coding:utf-8 _*_
Name:10_if02.py
Date:19-1-12
Author:westos-dz
Connect:
Desc:
"""
#1.定义变量age
age = 19
#2.判断是否满18岁
if age >= 18:
    print('你已经成年,欢迎来酒吧')
else:
    print('未成年,回家写作业')

 

#练习3

if-else逻辑运算符(and or)

age = 180
age = 110
"""
and
条件1 and 条件2
两个条件同时满足,就返回True
两个条件有一个不满足,就返回False
"""
# if age >= 0 and age <= 120:
#     print('正确')
# else:
#     print('错误')

"""
or:
条件1 or 条件2
两个条件只要有一个满足,就返回True
两个条件都不满足,返回False

"""

# python_score = 20
# # c_score = 30
# c_score = 70
# if python_score > 60 or c_score > 60:
#     print('考试通过')
# else:
#     print('准备补考')

#要判断用户的输入是否为空
value = input('Value:')
# if value == '':
#     print('请输入合法的值')
if not value:
    print('请输入合法的值')

 

#练习4

elif

if 要判断的条件:
    条件成立时,要做的事
elif 条件2:
    ...
else:
    条件都不成立时,要做的事

elif 和 else都必须和if联合使用,不能单独使用
"""
holiday_name = '情人节'

if holiday_name == '春节':
    print('吃饺子')
elif holiday_name == '元宵节':
    print('吃元宵')
elif holiday_name == '情人节':
    print('买礼物')
else:
    print('不过节')

#练习5

if嵌套

have_ticket = True

knife_length = 21

if have_ticket:
    print('车票检查通过,准备安检...')
    if knife_length > 20:
        print('刀长度为 %d:超出限定长度,禁止入内' %knife_length)
    else:
        print('刀长度为 %d:未超出限定长度,允许入内' %knife_length)
else:
    print('请先买票')

 

#练习6

if-elif-else语句

"""
# _*_coding:utf-8_*_
Name:practice1.py
Date:1/12/19
Author:westos-dz
Desc:猜拳游戏:电脑随机出拳,玩家选择出拳,得出猜拳结果
"""

print('please enter your choice: quan1 jiandao2 bu3')
choice = str(input('num:'))

import random             ##引入随机数

a = str(random.randint(1, 3))     ##石头剪刀布分别对应数字123



if choice == '1':
    if a == '1':
        print('diannao wei %s ,ping ju' % a)
    elif a == '2':
        print('diannao wei %s ,you win' % a)
    else:
        print('diannao wei %s ,you lose' % a)
elif choice == '2':
    if a == '1':
        print('diannao wei %s ,you lose' % a)
    elif a == '2':
        print('diannao wei %s ,ping ju' % a)
    else:
        print('diannao wei %s ,you win' % a)
else:
    if a == '1':
        print('diannao wei %s ,you win' % a)
    elif a == '2':
        print('diannao wei %s ,you lose' % a)
    else:
        print('diannao wei %s ,ping ju' % a)

#练习7

if-elif-else

"""
# _*_coding:utf-8_*_
Name:季节判断.py
Date:1/13/19
Author:westos-dz
Desc:用户输入月份,打印该月份所属的季节(3.4.5春季)
"""
month = int(input('请输入月份:'))

if (month==3 or month==4 or month ==5):
    print('%d月属于春季' %month)
elif (month ==6 or month ==7 or month==8):
    print('%d月属于夏季' %month)
elif (month==9 or month==10 or month ==11):
    print('%d月属于秋季' %month)
else:
    print('%d月属于冬季' %month)

 

#练习8

if-else判断输入是否为空

"""
# _*_coding:utf-8_*_
Name:是否为空.py
Date:1/13/19
Author:westos-dz
Desc:用if判断是否为空
"""
value = input ('Value:')
if value == '':
    print('输入为空')
else:
    print('输入不为空')

 

#练习9

if-else

"""
# _*_coding:utf-8_*_
Name:天数.py
Date:1/12/19
Author:westos-dz
Desc:输入年,月,输出本月有多少天
"""

year = int(input('请输入年份:'))
month = int(input('请输入月份'))

if (month == 2):
    if (year % 4 == 0 and year % 100 != 0) \
    or (year % 400 == 0):
        print('%d年的%d月有29天' %(year,month))
    else:
        print('%d年的%d月有28天' %(year,month))

elif (month == 7 or month == 8):
    print('%d年的%d月有31天' %(year,month))
elif (month < 7):
    if (month % 2 == 1):
        print('%d年的%d月有31天' %(year,month))
    else:
        print('%d年的%d月有30天' %(year,month))
else:
    if (month %2 == 1):
        print('%d年的%d月有30天' %(year,month))
    else:
        print('%d年的%d月有31天' %(year,month))

#练习10

if-else

"""
# _*_coding:utf-8_*_
Name:闰年判断.py
Date:1/12/19
Author:westos-dz
Desc:
"""

"""
判断闰年
用户输入年份,判断是否为闰年?
-能被400整除的是闰年,能被4整除但不能被100整除的是闰年
闰年2月29天
"""
year = int(input('请输入年份:'))

if (year % 4 == 0 and year % 100 != 0) \
        or (year % 400 == 0):
    print('%s是闰年' % year)
else:
    print('%s不是闰年' % year)

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值