chapter5 if语句

在这里插入图片描述

  • Python中区分大小写
cars = ['A']
for car in cars:
    if car == 'a':
        print(True)
    else:
        print(False)
-----------------------------------
false
  • 判断两值是否相等用(!=不相等)
  • 检查多个条件可以用and 或者 or (但是一个是与,一个是或)
  • 判断特定的值是否已包含在列表中,用关键字in 不在列表中,则用not in
  • 布尔表达式只有两种结果True /False
  • Python并不要求if-elif结构后面必须要有else代码块

课后习题

在这里插入图片描述

car = 'subaru'
print("Is car == 'subaru'?I predict True.")
print(car == 'subaru')

print("\nIs car == 'audi'?I predict Flase.")
print(car == 'audi')
-----------------------------------------------------------------
Is car == 'subaru'?I predict True.
True

Is car == 'audi'?I predict Flase.
False


在这里插入图片描述

"""检查两个字符串是否相等"""
apple = "Hello Python!"
dog = "Hello worl!"
print(apple == dog)
-------------------------------------------
False


""""使用lower()的测试"""
car = 'Lucy'
if car.lower() == 'lucy':
    print(True)
----------------------------
True

""""检查两个数相等,不等,大于,小于,大于等于和小于等于"""
num1 =  5
num2 = 10
if num1 == num2:
    print(True)
else:
    print(False)

if num1 > num2:
    print(True)
else:
    print(False)

if num1 >= num2:
    print(True)
else:
    print(False)
------------------------------------------------------------
False
False
False

在这里插入图片描述

alien_colors = ['green','yellow','red']
for alien_color in alien_colors:
 if (alien_color == 'green'):
    print("Player win 5 powers.")
 else:
    print("Nothing!")
-----------------------------------------------------
Player win 5 powers.
Nothing!
Nothing!

在这里插入图片描述

alien_colors = ['green','yellow','red']
for alien_color in alien_colors:
 if (alien_color == 'green'):
    print("Because of shotting the alien,player win 5 powers.")
 else:
    print("Player win 10 powers.")
--------------------------------------------------------------------------
Because of shotting the alien,player win 5 powers.
Player win 10 powers.
Player win 10 powers.

在这里插入图片描述

alien_colors = ['green','yellow','red']
for alien_color in alien_colors:
 if (alien_color == 'green'):
    print("green alien,player win 5 powers.")
 elif(alien_color == 'yellow'):
     print("yellow alien,player win 10 powers.")
 else:
    print("red alien,player win 15 powers.")
---------------------------------------------------------------------------
green alien,player win 5 powers.
yellow alien,player win 10 powers.
red alien,player win 15 powers.

在这里插入图片描述

age = 1
if age < 2:
    print('你是婴儿!')
elif 2 <= age < 4:
    print('你正在蹒跚学步!')
elif 4 <= age < 13:
    print('你是儿童!')
elif 13 <= age < 20:
    print('你是青少年!')
elif 20 <= age < 65:
    print('你是成年人!')
elif age >= 65:
    print('你是老年人!')

------------------------------------------------------
你是婴儿!

在这里插入图片描述

message = "You really like "
favorite_fruits = ['apples','bannanas','pears']
if 'apples' in favorite_fruits:
 print(message+'apples'+'!')
if 'bannanas' in favorite_fruits:
 print(message+'bannanas'+'!')
if 'pears' in favorite_fruits:
 print(message+'pears'+'!')
if 'c' in favorite_fruits:
 print(message+'c'+'!')
if 'd'in favorite_fruits:
 print(message+'d'+'!')
----------------------------------------------------------------
You really like apples!
You really like bannanas!
You really like pears!

在这里插入图片描述

users = ['admin','ben','jone','lily','kitty']
for user in users:
 if user == 'admin':
  print("Hello admin,would you like to see a status report?")
 else:
  print("Hello,"+user+" thank you for logging in again.")
-------------------------------------------------------------------------------
Hello admin,would you like to see a status report?
Hello,ben thank you for logging in again.
Hello,jone thank you for logging in again.
Hello,lily thank you for logging in again.
Hello,kitty thank you for logging in again.

在这里插入图片描述

user_name = []
if len(user_name) == 0:
	print('We need to find some users!')
for un in user_name:
	if un == 'admin':
		print('Hello admin, would you like to see a status report?')
	else:
		print('Hello, ' + un + ', thank you for logging in again')
----------------------------------------------------------------------------------
We need to find some users!

在这里插入图片描述

current_users = ['Alice','Ben','Jone','Lily','Cat']
new_users = ['Alice','Bob','Jone','Lucy','Mey']

for new_user in new_users:

 if new_user in current_users:
  print("We need to find new users!")
 else:
  print("The user "+new_user+" don't use.")
 -----------------------------------------------------------
 We need to find new users!
The user Lucy don't use.
The user Mey don't use.

!最后一点未实现

在这里插入图片描述

lists = list(range(1,10))
for list in lists:
	if list == 1:
		print('1st')
	elif list == 2:
		print('2nd')
	elif list == 3:
		print('3rd')
	else:
		print(str(list) + 'th')
------------------------------------------------------------
1st
2nd
3rd
4th
5th
6th
7th
8th
9th

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w要变强

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值