alien_color = 'green' if alien_color == 'green': print("You get 5 points.") elif alien_color == 'yellow': print("You get 10 points.") else: print("You get 15 points.") alien_color = 'yellow' if alien_color == 'green': print("You get 5 points.") elif alien_color == 'yellow': print("You get 10 points.") else: print("You get 15 points.") alien_color = 'red' if alien_color == 'green': print("You get 5 points.") elif alien_color == 'yellow': print("You get 10 points.") else: print("You get 15 points.")
5-6 人生的不同阶段 :设置变量 age 的值,再编写一个 if-elif-else 结构,根据 age 的值判断处于人生的哪个阶段。
如果一个人的年龄小于 2 岁,就打印一条消息,指出他是婴儿。
如果一个人的年龄为 2 (含)~ 4 岁,就打印一条消息,指出他正蹒跚学步。
如果一个人的年龄为 4 (含)~ 13 岁,就打印一条消息,指出他是儿童。
如果一个人的年龄为 13 (含)~ 20 岁,就打印一条消息,指出他是青少年。
如果一个人的年龄为 20 (含)~ 65 岁,就打印一条消息,指出他是成年人。
如果一个人的年龄超过 65 (含)岁,就打印一条消息,指出他是老年人。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
deftage(age): if age < 2: print("他是婴儿") elif age < 4: print("他正蹒跚学步") elif age < 13: print("他是儿童") elif age < 20: print("他是青少年") elif age < 65: print("他是成年人") else: print("他是老年人") tage(1) tage(3) tage(12) tage(19) tage(64) tage(65)
5-7 喜欢的水果 :创建一个列表,其中包含你喜欢的水果,再编写一系列独立的 if 语句,检查列表中是否包含特定的水果。
将该列表命名为 favorite_fruits ,并在其中包含三种水果。
编写 5 条 if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如 “You really like bananas!” 。
1 2 3 4 5 6 7 8 9
deftf(name): favorite_fruits = ["apple", "banana", "orange"] if name in favorite_fruits: print("You eally like %s!"%name) tf("apple") tf("banana") tf("orange") tf("pear") tf("strawberry")
如果用户名为 ‘admin’ ,就打印一条特殊的问候消息,如 “Hello admin, would you like to see a status report?” 。
否则,打印一条普通的问候消息,如 “Hello Eric, thank you for logging in again” 。
1 2 3 4 5 6
n = ["admin", "Eric", "Link", "Zelda", "Mifa"] for i in n: if i == 'admin': print("Hello admin, would you like to see a status report?") else: print("Hello %s, thank you for logging in again."%i)
5-9 处理没有用户的情形 :在为完成练习 5-8 编写的程序中,添加一条 if 语句,检查用户名列表是否为空。
如果为空,就打印消息 “We need to find some users!” 。
删除列表中的所有用户名,确定将打印正确的消息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
n = ["admin", "Eric", "Link", "Zelda", "Mifa"] if len(n) == 0: print("We need to find some users!") else: for i in n: if i == 'admin': print("Hello admin, would you like to see a status report?") else: print("Hello %s, thank you for logging in again."%i) n = [] if len(n) == 0: print("We need to find some users!") else: for i in n: if i == 'admin': print("Hello admin, would you like to see a status report?") else: print("Hello %s, thank you for logging in again."%i)
current_users = ["admin", "Eric", "Link", "Zelda", "Mifa"] new_users = ["Admin", "ErIc", "Mario", "Peach", "Komeji Koishi"] for i in new_users: if i.lower() in [j.lower() for j in current_users]: print("Please enter another name.") else: print("This username is not used.")
d = {"integer": "integer", "double": "double precision float point number", "float": "single precision float point number", "function": "a procedure process data", "variable": "a place which can be read and write"} for key, value in d.items(): print("%s\t: %s"%(key, value))
d = {"integer": "integer", "double": "double precision float point number", "float": "single precision float point number", "function": "a procedure process data", "variable": "a place which can be read and write", "bool": "bool", "char": "char", "short": "short integer", "long": "long integer", "long long": "long long integer"} for key, value in d.items(): print("%s\t: %s"%(key, value))
使用循环为每条河流打印一条消息,如 “The Nile runs through Egypt.” 。
使用循环将该字典中每条河流的名字都打印出来。
使用循环将该字典包含的每个国家的名字都打印出来。
1 2 3 4 5
r = {"Nile": "Egypt", "Huang": "China", "Chang": "China"} for key, value in r.items(): print("The %s runs through %s."%(key, value)) print(key) print(value)
p = ['jen', 'sarah', 'edward', 'phil', 'link'] favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', } for name in p: if name in favorite_languages: print("%s, thank you to attend this survey!"%name) else: print("%s, I want to invite you to do a survey."%name)
##6-7 人 :在为完成练习 6-1 而编写的程序中,再创建两个表示人的字典,然后将这三个字典都存储在一个名为 people 的列表中。遍历这个列表,将其中每个人的所有信息都打印出来。
1 2 3 4 5 6 7 8
l = [ {"first_name": "Riku", "last_name": "Dora", "age": 18, "city": "Unkown"}, {"first_name": "Shuvi", "last_name": "Dora", "age": 18, "city": "Unkown"}, {"first_name": "Steven", "last_name": "Dora", "age": 18, "city": "Unkown"} ] for info in l: for key, value in info.items(): print("%s\t: %s"%(key, value))
favorite_places = { "link": ["p1", "p2", "p3"], "zelda": ["p4", "p5", "p6"], "mifa": ["p7", "p8", "p9"] } for i, j in favorite_places.items(): print("%s : ["%i) for k in j: print("\t%s"%k) print("]")
num = { "Link": [1, 6], "Zelda": [2, 7], "Mifa": [3, 8], "Mario": [4, 9], "Peach": [5, 10] } for key, value in num.items(): print("%s likes ["%key) for i in value: print("\t%d"%i) print("]")
##6-11 城市 :创建一个名为 cities 的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该城市的事实。在表示每座城市的字典中,应包含 country 、 population 和 fact 等键。将每座城市的名字以及有关它们的信息都打印出来。
1 2 3 4 5 6 7 8 9 10
cities = { "name1": {"country": "country1", "population": "123456", "fact": "unkown"}, "name2": {"country": "country2", "population": "234567", "fact": "unkown"}, "name3": {"country": "country3", "population": "345678", "fact": "unkown"} } for i, j in cities.items(): print("%s : {"%i) for k, l in j.items(): print("\t%s : %s"%(k, l)) print("}")