教材第五章课后习题

代码:

#5.1
#print(5.1)
#print("一个简单的条件测试,可以理解比较后为何返回True或False")
#有点长,以及简单,就不写了

#5.2
print(5.2)
#前面几个都比较简单,就不写了
#写一下特定的值在列表中的查找
list2 = ["one","two","three",1,2,3]
if "one" in list2:
	print("one in list.")
if 2 in list2:
	print("2 in list.")
if "four" not in list2:
	print("four not in list.")
print()

#5.3
print(5.3)
colors = ['green','red','yellow']
alien_color = "green"
if alien_color == "green":
	print("you got 5 points.") 
print()

#5.4
print(5.4)
alien_color = "green"
if alien_color == "green":
	print("you got 5 points.(green)")
else:
	print("you got 10 points.(not green)")
alien_color = "yellow"
if alien_color == "green":
	print("you got 5 points.(green)")
else:
	print("you got 10 points.(not green)")
print()

#5.5
print(5.5)
for alien_color in colors:
	if alien_color == "green":
		print("you got 5 points.(green)")
	elif alien_color == "yellow":
		print("you got 10 points.(yellow)")
	else:
		print("you got 15 points.(red)")
print()

#5.6
print(5.6)
age = 18
print("age =",age)
if age < 2:
	print("婴儿")
elif 2 <= age < 4:
	print("蹒跚学步")
elif 4 <= age < 13:
	print("儿童")
elif 13 <= age < 20:
	print("青少年")
elif 20 <= age < 65:
	print("成年人")
elif 65 <= age:
	print("老年人")
print()

#5.7
print(5.7)
favorite_fruits = ["apple","orange","melon"]
if "beef" in favorite_fruits:
	print("You really like beef.")
if "banana" in favorite_fruits:
	print("You really like banana.")
if "apple" in favorite_fruits:
	print("You really like apple.")
if "orange" in favorite_fruits:
	print("You really like orange.")
if "melon" in favorite_fruits:
	print("You really like melon.")
print()

#5.8
print(5.8)
list8 = ['admin','boy','cat','dog','egg']
str8 = 'admin'
if str8 == 'admin':
	print("Hello admin,welcome back.")
else:
	print("Hello,",str8,".")
str8 = 'dog'
if str8 == 'admin':
	print("Hello admin,welcome back.")
else:
	print("Hello",end=" ")
	print(str8,end="")
	print(".")
print()

#5.9
print(5.9)
list8.clear()
if not list8:
	print("We need to find some users!")
else:
	if str8 == 'admin':
		print("Hello admin,welcome back.")
	else:
		print("Hello",end=" ")
		print(str8,end="")
		print(".")
print()

#5.10
print("5.10")
current_users = ["apple","banana","cat","dog","egg"]
new_users = ["ApPle","boy","cat","dinner","eleven"]
current_users_lower = [current_user.lower() for current_user in current_users]
for new_user in new_users:
	if new_user.lower() in current_users_lower:
		print("The user name has been used.")
	else:
		print("The user name hasn't been used.")
print()

#5.11
print(5.11)
list11 = []
for i in range(1,10):
	list11.append(i)
print(list11)
for i in list11:
	if i == 1:
		print("1st",end=" ")
	elif i == 2:
		print("2nd",end=" ")
	else:
		print(i,end="")
		print("th",end=" ")
print()

输出:

5.2
one in list.
2 in list.
four not in list.

5.3
you got 5 points.

5.4
you got 5 points.(green)
you got 10 points.(not green)

5.5
you got 5 points.(green)
you got 15 points.(red)
you got 10 points.(yellow)

5.6
age = 18
青少年

5.7
You really like apple.
You really like orange.
You really like melon.

5.8
Hello admin,welcome back.
Hello dog.

5.9
We need to find some users!

5.10
The user name has been used.
The user name hasn't been used.
The user name has been used.
The user name hasn't been used.
The user name hasn't been used.

5.11
[1, 2, 3, 4, 5, 6, 7, 8, 9]
1st 2nd 3th 4th 5th 6th 7th 8th 9th

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值