《Python编程》第五章部分课后练习题

#5-5 外星人颜色:

代码:

#5-5 外星人颜色
def get_points(alien_color):
	point = 0
	if alien_color == 'green':
		point = 5
	elif  alien_color == 'yellow':
		point = 10
	else:
		point = 25
	message = 'You get ' + str(point) + ' points!'
	print(message)

alien_color = 'green'
get_points(alien_color)

alien_color = 'yellow'
get_points(alien_color)

alien_color = 'red'
get_points(alien_color)

输出:

You get 5 points!
You get 10 points!
You get 25 points!


#5-8 以特殊方式跟管理员打招呼:

代码:

#5-8 以特殊方式跟管理员打招呼
users = ['Aaron', 'Caldwell', 'Kobe', 'Irving', 'admin']
users.append('James')
for user in users:
	message = ''
	if user == 'admin':
		message = "Hello admin, would you like to see a status report?"
	else:
		message = "Hello " + user + ", thank you for logging in again"
	print(message)

输出:

Hello Aaron, thank you for logging in again
Hello Caldwell, thank you for logging in again
Hello Kobe, thank you for logging in again
Hello Irving, thank you for logging in again
Hello admin, would you like to see a status report?
Hello James, thank you for logging in again


#5-10 检查用户名:

代码:

#5-10 检查用户名

current_users = ['Aaron', 'Caldwell', 'Kobe', 'Irving', 'James']
new_users = ['Kobe', 'Harden', 'JAMES', 'George', 'Davis']
for user in new_users:
	message = user.title()
	if user.title() in current_users:
		message += ', this name has been registered before,'
		message += 'Please try another one.'
	else:
		message += ', what a wonderful name!'
	print(message)

输出:

Kobe, this name has been registered before,Please try another one.
Harden, what a wonderful name!
James, this name has been registered before,Please try another one.
George, what a wonderful name!
Davis, what a wonderful name!

#5-11 序数:

代码:

#5-11 序数

for num in range(1, 10):
	message = str(num)
	if num == 1:
		message += 'st'
	elif num == 2:
		message += 'nd'
	elif num == 3:
		message += 'rd'
	else:
		message += 'th'
	print(message)

输出:

1st
2nd
3rd
4th
5th
6th
7th
8th
9th


你可能感兴趣的:(《Python编程》第五章部分课后练习题)