教材第五章练习题

这篇博客包含了多个编程练习,主要涉及对外星人颜色的判断(5-3到5-5)以及与用户相关的场景模拟,如处理没有用户的情况(5-9)、检查用户名(5-10)以及理解人生阶段(5-6)等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5-3 外星人颜色#1

alien_color = 'green'
if(alien_color == 'green'):
	print("You get 5 points!")
alien_color = 'yellow'
if(alien_color == 'green'):
	print("You get 5 points!")

输出

You get 5 points!

5-4 外星人颜色#2

alien_color = 'green'
if(alien_color == 'green'):
	print("You get 5 points!")
else:
	print("You get 10 points!")
alien_color = 'yellow'
if(alien_color == 'green'):
	print("You get 5 points!")
else:
	print("You get 10 points!")

输出

You get 5 points!
You get 10 points!

5-5 外星人颜色#3

alien_color = 'green'
if(alien_color == 'green'):
	print("You get 5 points!")
elif(alien_color == 'yellow'):
	print("You get 10 points!")
elif(alien_color == 'red'):
	print("You get 15 points!")
alien_color = 'yellow'
if(alien_color == 'green'):
	print("You get 5 points!")
elif(alien_color == 'yellow'):
	print("You get 10 points!")
elif(alien_color == 'red'):
	print("You get 15 points!")
alien_color = 'red'
if(alien_color == 'green'):
	print("You get 5 points!")
elif(alien_color == 'yellow'):
	print("You get 10 points!")
elif(alien_color == 'red'):
	print("You get 15 points!")

输出

You get 5 points!
You get 10 points!
You get 15 points!

5-6 人生的不同阶段

age = 17
if(age < 2):
	print("This person is a baby")
elif(age < 4):
	print("This person is learning walking")
elif(age < 13):
	print("This person is a child")
elif(age < 20):
	print("This person is a teenage")
elif(age < 65):
	print("This person is an adult")
else:
	print("This person is an elder")

输出

This person is a teenage

5-7 喜欢的水果

favorite_fruits = ['apple','watermelon','banana']
if('apple' in favorite_fruits):
	print("You really like apples!")
if('banana' in favorite_fruits):
	print("You really like bananas!")
if('watermelon' in favorite_fruits):
	print("You really like watermelons!")
if('peer' in favorite_fruits):
	print("You really like peers!")
if('orange' in favorite_fruits):
	print("You really like oranges!")
输出
You really like apples!
You really like bananas!
You really like watermelons!

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

users = ['admin','user1','user2','user3','Eric']
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")

输出

Hello admin, would you like to see a status report?
Hello user1, thank you for logging in again
Hello user2, thank you for logging in again
Hello user3, thank you for logging in again
Hello Eric, 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!")

输出

We need to find some users!

5-10 检查用户名

current_users = ['user1','user2','user3','user4','Eric']
new_users = ['user5','user6','user7','User1','eRic']
for new_user in new_users:
	for current_user in current_users:
		if(new_user.upper() == current_user.upper()):
			print("Refuse user name " + new_user)

输出

Refuse user name User1
Refuse user name eRic

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

输出

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值