Python编程:从入门到实践(课后习题5)

# 5-3 外星人颜色#1
alien_color = 'green'
if alien_color == 'green':
	print('You get 5 point.')
alien_color = 'yellow'
if alien_color == 'green':
	print('You get 5 point.')

# 5-4 外星人颜色#2
alien_color = 'yellow'
if alien_color == 'green':
	print('You get 5 point.')
else:
	print('You get 10 point.')

# 5-5 外星人颜色#3
alien_color = 'red'
if alien_color == 'green':
	print('You get 5 point.')
elif alien_color == 'yellow':
	print('You get 10 point.')
else:
	print('You get 15 point.')

# 5-6 人生的不同阶段
age = int(input('Enter your age: '))
if age < 2:
	print('You are baby.')
elif age >=2 and age < 4:
	print('You are toddler.')
elif age >=4 and age < 13:
	print('You are child.')
elif age >=13 and age < 20:
	print('You are teenager.')
elif age >=20 and age < 65:
	print('You are adult.')
elif age >= 65:
	print('You are elder.')
else:
	print('Your must enter a positive interger!')

# 5-6 喜欢的水果
favorite_fruits = ['bananas', 'apple', 'orange']
if 'bananas' in favorite_fruits:
	print('You really like ' + 'bananas!')
if 'apple' in favorite_fruits:
	print('You really like ' + 'apple!')
if 'orange' in favorite_fruits:
	print('You really like ' + 'orange!')
if 'mango' in favorite_fruits:
	print('You really like ' + 'mango!')
if 'pear' in favorite_fruits:
	print('You really like ' + 'pear!')

# 5-8 以特殊方式跟管理员打招呼
admins = ['admin', 'zhou', 'zhang', 'liu', 'li']
for admin in admins:
	if admin == 'admin':
		print("Hello admin, would you like to see a status report?")
	else:
		print("Hello " + admin + ", thank you for logging in again.")

# 5-9 处理没有用户的情形
if admins:
	for admin in admins:
		if admin == 'admin':
			print("Hello admin, would you like to see a status report?")
		else:
			print("Hello " + admin + ", thank you for logging in again.")
else:
	print("We need to find some users!")  # 5-9-1

admins = []  # 5-9-2 重复5-9-1的代码

# 5-10 检查用户名
current_users = ['kain', 'chow', 'lily', 'zhou', 'zhang']  # 5-10-1
new_users = ['Kain', 'lily', 'Jie', 'peng', 'tj']  # 5-10-2
for new_user in new_users:
	if new_user.lower() in current_users:
		print("You need enter another username!")
	else:
		print("This username is not used.")

# 5-11 序数
list1 = list(range(1, 10))
list2 = [x for x in range(1, 10)]
for i in list1:
	if i == 1:
		print(str(i) + 'st')
	elif i == 2:
		print(str(i) + 'nd')
	else:
		print(str(i) + 'th')

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值