第五章—if语句

5-1 条件测试 :编写一系列条件测试;将每个测试以及你对其结果的预测和实际结果都打印出来。你编写的代码应类似于下面这样:

car = 'subaru'

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

 print(car == 'subaru')    

print("\nIs car == 'audi'? I predict False.")

 print(car == 'audi')

#详细研究实际结果,直到你明白了它为何为True 或False 。 

#创建至少10个测试,且其中结果分别为True 和False 的测试都至少有5个。 

1 car = 'subaru'
2 print("Is car == 'subarur'? I predict True.")
3 print(car == 'subaru')
4 
5 print("\nIs car == 'audi'? I predict False")
6 print(car == 'audi')

输出:

1 Is car == 'subarur'? I predict True.
2 True
3 
4 Is car == 'audi'? I predict False
5 False

5-2 更多的条件测试:你并非只能创建10个测试。如果你想尝试做更多的比较,可再编写一些测试,并将它们加入到conditional_test.py中。对于下面列出的各种测试,至少编写一个结果为True和False的测试。

#检查两个字符串相等不相等。

#使用函数lower()的测试。

#检查两个数字相等、不等、大于、小于、大于等于和小于等于。

#使用关键字and和or的测试。

#测试特定的值是否包含在列表中。

#测定特定的值是否未包含在列表中。

 1 str1='aa'
 2 str2='bb'
 3 print(str1==str2)
 4 print(str1!=str2)
 5 name1='Ada'
 6 print(name1.lower()=='ada')
 7 num1=5
 8 num2=8
 9 print(num1==num2)
10 print(num1!=num2)
11 print(num1>num2)
12 print(num1<num2)
13 print(num1>=num2)
14 print(num1<=num2)
15 print(num1>=2 and num2>=2)
16 print(num1<=6 or num2<=6)
17 info=['a','b','c']
18 print('a' in info)
19 print('d' not in info)

输出:

 1 False
 2 True
 3 True
 4 False
 5 True
 6 False
 7 True
 8 False
 9 True
10 True
11 True
12 True
13 True

5-3  外星人颜色#1 :假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为'green' 、'yellow' 或'red' 。

 #编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。

#编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。

1 alien1="green"
2 if alien1=="green":
3     print("You get 5 point because you killed  an alien.")
4 alien2="red"
5 if alien2=="green":
6     print("You get 5 point because you killed  an alien.")

输出:

You get 5 point because you killed  an alien.

5-4 外星人颜色#2 :像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。

 #如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点。

 #如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。

 #编写这个程序的两个版本,在一个版本中执行if 代码块,而在另一个版本中执行else 代码块。

 1 alien1="green"
 2 if alien1=="green":
 3     print("You get 5 point because you killed  an alien.")
 4 else :
 5     print("You get 10 point.")
 6 alien2="red"
 7 if alien2=="green":
 8     print("You get 5 point because you killed  an alien.")
 9 else :
10     print("You get 10 point.")

输出:

1 You get 5 point because you killed  an alien.
2 You get 10 point.

5-5  外星人颜色#3 :将练习5-4中的if-else 结构改为if-elif-else 结构。

 #如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。

 #如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。

 #如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。

 #编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。

 1 alien='green'
 2 if alien =='green':
 3     print('You get 5 point.')
 4 elif alien =='yellow':
 5     print('You get 10 point.')
 6 else :
 7     print('You get 15 point.')
 8 
 9 alien2='yellow'
10 if alien2=='green':
11     print('You get 5 point.')
12 elif alien2=='yellow':
13     print('You get 10 point.')
14 else:
15     print('You get 15 point.')
16 
17 alien3='red'
18 if alien3=='green':
19     print('You get 5 point.')
20 elif alien3=='yellow':
21     print('You get 10 point.')
22 else :
23     print('You get 15 point.')

输出:

You get 5 point.
You get 10 point.
You get 15 point.

5-6 人生的不同阶段 :设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。

 # 如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。

 # 如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。

 #如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。

 #如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。

 # 如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。

 # 如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。

 1 age=25
 2 if age<2:
 3     print("He is a baby. ")
 4 elif age>=2 and age <4:
 5     print("He is learning how to walk. ")
 6 elif age>=4 and age<13:
 7     print("He is a child. ")
 8 elif age>=13 and age <20:
 9     print("He is a young man. ")
10 elif age >=20 and age<65:
11     print("He is an adult. ")
12 else :
13     print("He is an old man. ")

输出:

1 He is an adult.

5-7 喜欢的水果 :创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if 语句,检查列表中是否包含特定的水果。

 #将该列表命名为favorite_fruits ,并在其中包含三种水果。

 #编写5条if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如“You really like bananas!”。 

 1 favorite_fruits=['apple','banana','orange']
 2 if 'apple' in favorite_fruits:
 3     print('You really like apples!')
 4 if 'banana' in favorite_fruits:
 5     print('You really like bananas!')
 6 if 'orange' in favorite_fruits:
 7     print('You really like oranges!')
 8 if 'grape' in favorite_fruits:
 9     print("You really like grapes.")
10 if 'pear' in favorite_fruits:
11     print('You really like pears.')

输出:

1 You really like apples!
2 You really like bananas!
3 You really like oranges!

5-8 以特殊方式跟管理员打招呼 :创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每位用户登录网站后都打印一条问 候消息。遍历用户名列表,并向每位用户打印一条问候消息。

 #如果用户名为'admin' ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。

 #否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。

1 usernames=['admin','ada','ava','peter','Marry']
2 for username in usernames:
3     if username=='admin':
4         print("Hello admin,would you like to see a status report?")
5     else:
6         print('Hello '+username+',thank you for loging in again.')

输出:

1 Hello admin,would you like to see a status report?
2 Hello ada,thank you for loging in again.
3 Hello ava,thank you for loging in again.
4 Hello peter,thank you for loging in again.
5 Hello Marry,thank you for loging in again.

5-9 处理没有用户的情形 :在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。

 #如果为空,就打印消息“We need to find some users!”。

 #删除列表中的所有用户名,确定将打印正确的消息。

1 usernames=[]
2 if usernames:
3     for username in usernames:
4         if username=='admin':
5             print("Hello admin,would you like to see a status report?")
6         else:
7             print('Hello '+username+',thank you for loging in again.')
8 else:
9     print("We need some users.")

输出:

We need some users.

5-10 检查用户名 :按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。

 # 创建一个至少包含5个用户名的列表,并将其命名为current_users 。

 #再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。

 #遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指 出这个用户名未被使  用。 确保比较时不区分大消息;换句话说,如果用户名'John' 已被使用,应拒绝用户名'JOHN' 。

1 current_users=['a','b','c','d','e','h']
2 new_users=['A','b','f','g','p']
3 for new_user in new_users :
4     if new_user.lower() in current_users:
5         print("You should print other name.")
6     else :
7         print("This name can use.")

输出:

1 You should print other name.
2 You should print other name.
3 This name can use.
4 This name can use.
5 This name can use.

5-11  序数 :序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。

 #在一个列表中存储数字1~9。

 # 遍历这个列表。 

 #在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序 数都独占一行。

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

输出:

1st
2nd
3rd
4th
5th
6th
7th
8th
9th

猜你喜欢

转载自www.cnblogs.com/liuhaier/p/8909522.html