您的位置:首页 > 编程语言 > Python开发

python第三周作业——chapter 5:if语句,chapter 6:字典

2018-03-22 23:19 387 查看

Chapter 5

本章主要学习条件判断,简单的if语句,if语句用于列表处理等
Q 5-2 对于下面列出的各种测试, 至少编写一个结果为True 和False 的测试。
检查两个字符串相等和不等。
使用函数lower() 的测试。
检查两个数字相等、 不等、 大于、 小于、 大于等于和小于等于。
使用关键字and 和or 的测试。
测试特定的值是否包含在列表中。
测试特定的值是否未包含在列表中。

知识点:条件判断
代码:#test 1: string
car1 = 'Subaru'
car2 = 'Audi'
car3 = 'Audi'
print(car1==car2)
print(car2==car3)

#test 2: lower()
car4 = 'audi'
print("\n")
print(car2==car4)
print(car2.lower()==car4)

#test 3: number
print("\n")
print(6==7)
print(19==19)

#test 4: and,or
print("\n")
x=10
print(x==10 and x==5)
print(x==10 or x==5)

#test 5,6: list
print("\n")
list = {1,3,6,9,11}
print(1 in list)
print(4 in list)
print(1 not in list)
print(4 not in list)运行结果:False
True

False
True

False
True

False
True

True
False
False
True


Q 5-6 人生的不同阶段 : 设置变量age 的值, 再编写一个if-elif-else 结构, 根据age 的值判断处于人生的哪个阶段。
如果一个人的年龄小于2岁, 就打印一条消息, 指出他是婴儿。
如果一个人的年龄为2(含) ~4岁, 就打印一条消息, 指出他正蹒跚学步。
如果一个人的年龄为4(含) ~13岁, 就打印一条消息, 指出他是儿童。
如果一个人的年龄为13(含) ~20岁, 就打印一条消息, 指出他是青少年。
如果一个人的年龄为20(含) ~65岁, 就打印一条消息, 指出他是成年人。
如果一个人的年龄超过65(含) 岁, 就打印一条消息, 指出他是老年人。

知识点:if-elif-else 语句
代码:age = 20
if age<2:
print("The person is a baby.")
elif age<4:
print("The person is a loddler.")
elif age<13:
print("The person is a kid.")
elif age<20:
print("The person is a teenager.")
elif age<65:
print("The person is an adult.")
else:
print("The person is an elder.")运行结果:The person is an adult.

Q 5-10 检查用户名 : 按下面的说明编写一个程序, 模拟网站确保每位用户的用户名都独一无二的方式。
创建一个至少包含5个用户名的列表, 并将其命名为current_users 。
再创建一个包含5个用户名的列表, 将其命名为new_users , 并确保其中有一两个用户名也包含在列表current_users 中。
遍历列表new_users , 对于其中的每个用户名, 都检查它是否已被使用。 如果是这样, 就打印一条消息, 指出需要输入别的用户名; 否则, 打印一条消息, 指出这个用户名未被使用。
确保比较时不区分大小写; 换句话说, 如果用户名'John' 已被使用, 应拒绝用户名'JOHN' 。

知识点:if语句用于列表处理
代码:current_users = ['Alice','Cindy','David','John','lucy']
new_users = ['JOHN','Lucy','unknown','Rose','Hawking']
i=0
while i<len(current_users):
current_users[i] = current_users[i].title()
i=i+1
#print(current_users)
for user in new_users:
if user.title() in current_users:
print("The name exists.Please enter a new username.")
else:
print("The username is available.")(为解决大小写问题,我先把current_users中的元素用title()统一格式,再进行比较)
运行结果:The name exists.Please enter a new username.
The name exists.Please enter a new username.
The username is available.
The username is available.
The username is available.

Chapter 6

本章主要学习python中的字典,包括字典的建立、访问、修改、遍历、嵌套。
Q 6-2 喜欢的数字 : 使用一个字典来存储一些人喜欢的数字。 请想出5个人的名字, 并将这些名字用作字典中的键; 想出每个人喜欢的一个数字, 并将这些数字作为值存储在字典中。 打印每个人的名字和喜欢的数字。

知识点:字典的建立和元素访问,遍历
代码:#6-2 favorite numbers
favorite_numbers = {'Alice':3,'Tom':7,'John':1,'Lvy':6,'Ella':5}
for name,number in favorite_numbers.items():
print(name.title() + "'s favorite number is " + str(number))运行结果:Alice's favorite number is 3
Tom's favorite number is 7
John's favorite number is 1
Lvy's favorite number is 6
Ella's favorite number is 5

Q 6-5 河流 : 创建一个字典, 在其中存储三条大河流及其流经的国家。 其中一个键—值对可能是'nile': 'egypt' 。
使用循环为每条河流打印一条消息, 如“The Nile runs through Egypt.”。
使用循环将该字典中每条河流的名字都打印出来。

使用循环将该字典包含的每个国家的名字都打印出来。
知识点:字典遍历,key遍历,value遍历
代码:#6-5 rivers
rivers = {'nile':'egypt','amazon river':'brazil','changjiang river':'china'}
for river, country in rivers.items():
print(river.title() + " runs through " + country +".")
print('\n')
for river in rivers.keys():
print(river.title())
print('\n')
for country in rivers.values():
print(country.title())运行结果:Nile runs through egypt.
Amazon River runs through brazil.
Changjiang River runs through china.

Nile
Amazon River
Changjiang River

Egypt
Brazil
China
4000


Q 6-11 城市 : 创建一个名为cities 的字典, 其中将三个城市名用作键; 对于每座城市, 都创建一个字典, 并在其中包含该城市所属的国家、 人口约数以及一个有关该城市的事实。 在表示每座城市的字典中, 应包含country 、 population 和fact 等键。 将每座城市的名字以及有关它们的信息都打印出来。

知识点:字典嵌套
代码:#6-11
cities = {'Beijing':{'country':'China','population':20000000,'fact':'has a long history.'},
'New York':{'country':'America','population':8500000,'fact':'is the capital of America.'},
'Tokyo':{'country':'Japan','population':37000000,'fact':'is the capital of Japan.'}}
for city,information in cities.items():
print(city + " is in " + information['country'])
print("The population of the city is about " + str(information['population']))
print(city + " " + information['fact'] + '\n')运行结果:eijing is in China
The population of the city is about 20000000
Beijing has a long history.

New York is in America
The population of the city is about 8500000
New York is the capital of America.

Tokyo is in Japan
The population of the city is about 37000000
Tokyo is the capital of Japan.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python