本文代码是在jupyter中实现的,仅为了自我督促学习python之用。
5-3 外星人颜色#1:假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为 ‘green’ 、 ‘yellow’ 或 ‘red’ 。
编写一条 if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了 5个点。
编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。
代码:
alien_color = 'green'
if alien_color=='green' :
print("Congratulations, get 5 experience points! ")
运行结果:
Congratulations, get 5 experience points!
由于一开始if后面的条件语句写错了,结果导致运行出错。具体如下:
错误代码:
alien_color = 'green'
if alien_color='green' :
print("Congratulations, get 5 experience points! ")
运行报错:
File "<ipython-input-11-0d8cd715f4a9>", line 2
if alien_color='green' :
^
SyntaxError: invalid syntax
代码:
alien_color = 'red'
if alien_color=='green' :
print("Congratulations, get 5 experience points! ")
运行结果:
此处由于,条件不符合,所以没有执行 if 底下的语句,无输出结果。
5-4 外星人颜色#2:像练习 5-3那样设置外星人的颜色,并编写一个 if-else 结构。
如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了 5 个点。
如果外星人不是绿色的,就打印一条消息,指出玩家获得了 10个点。
编写这个程序的两个版本,在一个版本中执行 if 代码块,而在另一个版本中执行 else 代码块。
代码:
alien_color = 'green'
if alien_color=='green' :
print("Congratulations, get 5 experience points! ")
else:
print('Congratulations, get 10 experience points!')
运行结果:
Congratulations, get 5 experience points!
代码:
alien_color = 'red'
if alien_color=='green' :
print("Congratulations, get 5 experience points! ")
else:
print('Congratulations, get 10 experience points!')
运行结果:
Congratulations, get 10 experience points!
5-5 外星人颜色#3:将练习 5-4中的 if-else 结构改为 if-elif-else 结构。
如果外星人是绿色的,就打印一条消息,指出玩家获得了 5个点。
如果外星人是黄色的,就打印一条消息,指出玩家获得了 10个点。
如果外星人是红色的,就打印一条消息,指出玩家获得了 15个点。
编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。
代码:
alien_color = 'green'
if alien_color=='green' :
print("Congratulations, get 5 experience points! ")
elif alien_color=='yello':
print("Congratulations, get 10 experience points! ")
else:
print('Congratulations, get 15 experience points!')
运行结果:
Congratulations, get 5 experience points!
代码:
alien_color = 'yello'
if alien_color=='green' :
print("Congratulations, get 5 experience points! ")
elif alien_color=='yello':
print("Congratulations, get 10 experience points! ")
else:
print('Congratulations, get 15 experience points!')
运行结果:
Congratulations, get 10 experience points!
代码:
alien_color = 'red'
if alien_color=='green' :
print("Congratulations, get 5 experience points! ")
elif alien_color=='yello':
print("Congratulations, get 10 experience points! ")
else:
print('Congratulations, get 15 experience points!')
运行结果:
Congratulations, get 15 experience points!
5-6 人生的不同阶段:设置变量 age 的值,再编写一个 if-elif-else 结构,根据 age的值判断处于人生的哪个阶段。
如果一个人的年龄小于 2岁,就打印一条消息,指出他是婴儿。
如果一个人的年龄为 2(含)~4岁,就打印一条消息,指出他正蹒跚学步。
如果一个人的年龄为 4(含)~13岁,就打印一条消息,指出他是儿童。
如果一个人的年龄为 13(含)~20岁,就打印一条消息,指出他是青少年。
如果一个人的年龄为 20(含)~65岁,就打印一条消息,指出他是成年人。
如果一个人的年龄超过 65(含)岁,就打印一条消息,指出他是老年人。
代码:
age = 15
if age < 2:
level = 'baby'
elif 2 <= age < 4:
level = 'Toddler'
elif 4 <= age < 13:
level = 'child'
elif 13 <= age < 20:
level = 'adolescent'
elif 20 <= age < 65:
level = 'adult'
else:
level = 'old'
print(level)
运行结果:
adolescent
5-7 喜欢的水果:创建一个列表,其中包含你喜欢的水果,再编写一系列独立的 if语句,检查列表中是否包含特定的水果。
将该列表命名为 favorite_fruits ,并在其中包含三种水果。
编写 5条 if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如“You really like bananas!”。
代码:
favorite_fruits = ['apple', 'bananas', 'orange']
if 'apple' in favorite_fruits:
print("You really like apple!")
if 'watermelon' in favorite_fruits:
print("You really like watermelon!")
if 'bananas' in favorite_fruits:
print("You really like bananas!")
if 'pineapple' in favorite_fruits:
print("You really like pineapple!")
if 'orange' in favorite_fruits:
print("You really like orange!")
运行结果:
You really like apple!
You really like bananas!
You really like orange!
总之,如果你只想执行一个代码块,就使用 if-elif-else 结构;如果要运行多个代码块,就使用一系列独立的 if 语句。