Python编程 从入门到实践 练习5-8~5-11

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

# 以特殊方式跟管理员打招呼
users = ['admin', 'alice', 'bob', 'carlos', 'david']

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.")

Python编程 从入门到实践 练习5-8~5-11_第1张图片

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!")

Python编程 从入门到实践 练习5-8~5-11_第2张图片

5-10 检查用户名

# 检查用户名
current_users = ['alice', 'bob', 'carmen', 'david', 'eric']
new_users = ['Alice', 'david', 'ford', 'gloria', 'harris']

for user in new_users:
	if user.lower() in current_users:
		print(user + " have been used.Please create a new name.")
	else:
		print(user + " is not be used.")

Python编程 从入门到实践 练习5-8~5-11_第3张图片

5-11 序数

# 序数
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers:
	if number == 1:
		print("1st")
	elif number == 2:
		print("2nd")
	elif number == 3:
		print("3rd")
	else:
		print(str(number) + "th")

Python编程 从入门到实践 练习5-8~5-11_第4张图片

你可能感兴趣的:(python编程从入门到实践)