python作业03-条件语句&字典

Description (if)

Select from exercise 5-1 to 5-13

Code (if)

5-3

1
2
3
4
5
6
7
8
9
10
11
# 5-3 外星人颜色#1 :假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,
# 并将其设置为'green' 、'yellow' 或'red' 。
# 编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。
# 编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。

alien_color = "green"
if alien_color == "green":
print("Get 5 points!")
alien_color = "yellow"
if alien_color == "green":
print("Get 5 points!")

result:

1
Get 5 points!

5-6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 5-6 人生的不同阶段 :设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。
def howold(age):
if age<2:
print("a baby")# 如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。
elif age<4:
print("learning how to walk")# 如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。
elif age<13:
print("a child")# 如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。
elif age<20:
print("a teenage")# 如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。
elif age<65:
print("an adult")# 如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。
else:
print("the old")# 如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。
howold(1)
howold(3)
howold(5)
howold(18)
howold(24)
howold(90)

result:

1
2
3
4
5
6
a baby
learning how to walk
a child
a teenage
an adult
the old

5-8

1
2
3
4
5
6
7
8
9
10
11
# 5-8 以特殊方式跟管理员打招呼 :创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。
# 想象你要编写代码,在每位用户登录网站后都打印一条问
# 候消息。遍历用户名列表,并向每位用户打印一条问候消息。
# 如果用户名为'admin' ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。
# 否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。
users = ["Eric", "Kris", "Smith", "admin", "Harry"]
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!")

result:

1
2
3
4
5
Hello Eric, thank you for logging in again!
Hello Kris, thank you for logging in again!
Hello Smith, thank you for logging in again!
Hello admin, would you like to see a status report?
Hello Harry, thank you for logging in again!

5-9

1
2
3
4
5
6
7
8
9
10
11
12
# 5-9 处理没有用户的情形 :在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。
# 如果为空,就打印消息“We need to find some users!”。
# 删除列表中的所有用户名,确定将打印正确的消息
users = []
if users:
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!")
else:
print("We need to find some users!")

result:

1
We need to find some users!

5-11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 5-11 序数 :序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。
# 在一个列表中存储数字1~9。
# 遍历这个列表。
# 在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、
# 6th 、7th 、8th 和9th ,但每个序数都独占一行。
numbers = range(1, 10)
for number in numbers:
if number == 1:
print('1st')
elif number == 2:
print('2nd')
elif number == 3:
print('3rd')
else:
print(str(number)+'th')

result:

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

Description (dictionary)

Select from exercise 6-1 to 5-13

Code (dictionary)

6-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 6-3 词汇表 :Python字典可用于模拟现实生活中的字典,但为避免混淆,我们将后者称为词汇表。
# 想出你在前面学过的5个编程词汇,将它们用作词汇表中的键,并将它们的含义作为值存储在词汇表中。
# 以整洁的方式打印每个词汇及其含义。为此,你可以先打印词汇,在它后面加上一个冒号,再打印词汇的含义;也可在一行打印词汇,
# 再使用换行符(\n )插入一个空行,然后在下一行以缩进的方式打印词汇的含义。

words = {
'if': 'if what happen then do something',
'while': 'loop',
'or': 'in bool, A happens or B happens are both fine',
'var': 'record some number, string, dictionary etc',
'python': 'easy to get star'
}
print('python: '+words['python']+'.')
print('if: '+words['if']+'.')

result:

1
2
python: easy to get star.
if: if what happen then do something.

6-4

1
2
3
4
5
6
7
8
9
10
11
# 6-4 词汇表2 :既然你知道了如何遍历字典,现在请整理你为完成练习6-3而编写的代码,将其中的一系列print
# 语句替换为一个遍历字典中的键和值的循环。确定该循环正确无误后,再在词汇表中添加5个Python术语。
# 当你再次运行这个程序时,这些新术语及其含义将自动包含在输出中。

words['import'] = 'as include in c'
words['for'] = 'another loop'
words['and'] = 'only if A and B both true'
words['class'] = 'var define by coder'
words['print'] = 'to print string and begin a new line'
for key in words.keys():
print(key + ': '+words[key]+'.')

result:

1
2
3
4
5
6
7
8
9
10
if: if what happen then do something.
while: loop.
or: in bool, A happens or B happens are both fine.
var: record some number, string, dictionary etc.
python: easy to get star.
import: as include in c.
for: another loop.
and: only if A and B both true.
class: var define by coder.
print: to print string and begin a new line.

6-8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#6-8 宠物 :创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,
# 包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets的列表中,再遍历该列表,并将宠物的所有信息都打印出来。

pets = [{"name": "Kingkong",
"type": "dog",
"master": "Tony"},
{"name": "Kitty",
"type": "cat",
"master": "Jessica"},
{"name": "Big Bird",
"type": "snake",
"master": "Jason"},
{"name": "Son",
"type": "kangaroo",
"master": "Micheal"},
]
for pet in pets:
print(pet["name"]+" is a "+pet["type"]+", it is "+pet["master"]+"'s friend!")

result:

1
2
3
4
Kingkong is a dog, it is Tony's friend!
Kitty is a cat, it is Jessica's friend!
Big Bird is a snake, it is Jason's friend!
Son is a kangaroo, it is Micheal's friend!