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编程——从入门到实践》第五章部分习题解
- 《Python编程 从入门到实践》第五章课后习题
- 《Python编程 从入门到实践》第五章习题选做
- 【《Python编程: 从入门到实践》习题答案--第五章:<if语句>】
- python编程从入门到实践第五章习题
- 《Python编程 从入门到实践》第六章习题选做
- 《python编程从入门到实践》 第8章习题选做
- 《Python编程 从入门到实践》第八章习题选做
- 《Python编程 从入门到实践》第十章习题选做
- 【Python编程:从入门到实践】第五章:if语句
- [Python编程:从入门到实践] 第十五章:生成数据 习题答案
- [Python编程:从入门到实践] 第十六章:下载数据 习题答案
- 【《Python编程: 从入门到实践》习题答案--第十六章,番外2<No module named 'pygal.i18n>】
- python编程:从入门到实践-第五章练习
- python编程从入门到实践 习题11-3
- 《Python编程 从入门到实践》第八章课后习题
- Python编程:从入门到实践(课后习题9)
- 《Python编程 从入门到实践》第四章习题选做
- 《Python编程——从入门到实践》第二章习题解
- 《Python编程-从入门到实践》第2章习题选练