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

《Python编程 从入门到实践》第五章习题选做

2018-03-20 20:53 309 查看
5-3 外星人颜色#1alien_color = 'green'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")

alien_color = 'red'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")输出:You have killed green alien and got 5 points!5-4 外星人颜色#2alien_color = 'green'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 10 points!")

alien_color = 'red'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 10 points!")输出:You have killed green alien and got 5 points!
You have killed red alien and got 10 points!5-5 外星人颜色#3alien_color = 'green'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
elif alien_color == 'yellow':
print("You have killed " + alien_color + " alien" + " and got 10 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 15 points!")

alien_color = 'yellow'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
elif alien_color == 'yellow':
print("You have killed " + alien_color + " alien" + " and got 10 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 15 points!")

alien_color = 'red'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
elif alien_color == 'yellow':
print("You have killed " + alien_color + " alien" + " and got 10 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 15 points!")输出:You have killed green alien and got 5 points!
You have killed yellow alien and got 10 points!
You have killed red alien and got 15 points!5-6 人生的不同阶段age = 34
if age < 2:
print("You are a baby.")
elif age >= 2 and age < 4:
print("You are toddling.")
elif age >= 4 and age < 13:
print("You are a child.")
elif age >= 13 and age < 20:
print("You are a teenager.")
elif age >= 20 and age < 65:
print("You are an adult.")
else:
print("You are an old man.")输出:You are an adult.5-8 以特殊方式跟管理员打招呼users = ['Mike', 'John', 'admin', 'Amy', 'Ten']
for user in users:
if user == 'admin':
print("Hello " + user + ", would you like to see a status report?")
else:
print("Hello " + user + ", thank you for logging in again.")输出:Hello Mike, thank you for logging in again.
Hello John, thank you for logging in again.
Hello admin, would you like to see a status report?
Hello Amy, thank you for logging in again.
Hello Ten, thank you for logging in again.5-10 检查用户名current_users = ['Mike', 'John', 'admin', 'Amy', 'Ten']
new_users = ['Sarah', 'Newton', 'John', 'Ten', 'Hua']
lower_users = []
for user in current_users:
lower_users.append(user.lower())
for user in new_users:
if user.lower() in lower_users:
print("This name has been used, please enter another username.")
else:
print("The name has not been used yet.")输出:The name has not been used yet.
The name has not been used yet.
This name has been used, please enter another username.
This name has been used, please enter another username.
The name has not been used yet.5-11 序数nums = list(range(1, 10))
for num in nums:
if num == 1:
print(str(num) + "st")
elif num == 2:
print(str(num) + "nd")
elif num == 3:
print(str(num) + "rd")
else:
print(str(num) + "th")输出:1st
2nd
3rd
4th
5th
6th
7th
8th
9th
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python