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

Python编程:从入门到实践 第五章习题解析

2018-03-25 20:29 375 查看
已经标有题号了,就不额外多讲了。

#5-2
str1 = "abc"
str2 = "Abc"
str3 = "ABC"
n1   = 10
n2   = 11

print(str1 == str2)
print(str1 == str2.lower())
print(n1   <  n2)
print(n1   <=  n2)
print(n1   ==  n2)
print(n1   >  n2)
print(n1   >=  n2)
print(n1 <= n2 and str1 == str3.lower())
print(n1 < n2 or str1 == str2)
print('a' in str2)
print('a' not in str1) #regard string as a list of chars

#5-3
colors = ["green","yellow","red"]
alien_color1 = colors[1]
alien_color2 = colors[0]

if alien_color1 == "green":
print("5 poionts!")
if alien_color2 == "green":
print("5 poionts!")

#5-4
if alien_color1 == "green":
print("5 poionts!")
else: print("10 points!")
if alien_color2 == "green":
print("5 poionts!")
else: print("10 points!")

#5-5
alien_color3 = colors[2]
if alien_color1 == "green":
print("5 poionts!")
elif alien_color1 == "yellow":
print("10 poionts!")
elif alien_color1 == "red":
print("15 poionts!")
if alien_color2 == "green":
print("5 poionts!")
elif alien_color2 == "yellow":
print("10 poionts!")
elif alien_color2 == "red":
print("15 poionts!")
if alien_color3 == "green":
print("5 poionts!")
elif alien_color3 == "yellow":
print("10 poionts!")
elif alien_color3 == "red":
print("15 poionts!")

#5-6
age = 19
if age < 2:
print("You are a baby~")
elif age <4:
print("You are learing walking.")
elif age <13:
print("You are a kid.")
elif age <20:
print("You are a teenager.")
elif age <65:
print("You are an adult.")
elif age >=65:
print("You are an elder.")

#5-7
favorite_fruits = ["orange","pear","apple"]
if "orange" in favorite_fruits:
print("You realy like oranges!")
if "pear" in favorite_fruits:
print("You realy like pears!")
if "apple" in favorite_fruits:
print("You realy like apples!")
if "grasp" in favorite_fruits:
print("You realy like grasps!")
if "bananna" in favorite_fruits:
print("You realy like banannas!")

#5-8
users = ["A",'B','C','D','admin']
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")

#5-9
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!")

#5-10
current_users = ["A",'B','C','D','admin']
new_users = ["admin","B","E","F","GH"]
if new_users:
for new_user in new_users:
if new_user.lower() in [u.lower() for u in current_users]:
print("NAME HAS BEEN USED. TRY another one.")
else :
print("NAME HAS NOT BEEN USED.")


输出:

===================== RESTART: D:/program/Python/第五章.py =====================
False
True
True
True
False
False
False
True
True
False
False
5 poionts!
10 points!
5 poionts!
10 poionts!
5 poionts!
15 poionts!
You are a teenager.
You realy like oranges!
You realy like pears!
You realy like apples!
Hello, A thank you for logging in again
Hello, B thank you for logging in again
Hello, C thank you for logging in again
Hello, D thank you for logging in again
Hello admin, would you like to see a status report?
We need to find some users!
NAME HAS BEEN USED. TRY another one.
NAME HAS BEEN USED. TRY another one.
NAME HAS NOT BEEN USED.
NAME HAS NOT BEEN USED.
NAME HAS NOT BEEN USED.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 编程