Python作业(5.1-5.11)

Python第五章作业

5-2 更多的条件测试 : 你并非只能创建 10 个测试。 如果你想尝试做更多的比较, 可再编写一些测试, 并将它们加入到 conditional_tests.py 中。 对于下面列出的各种测
试, 至少编写一个结果为
True False 的测试。
检查两个字符串相等和不等。
使用函数
lower() 的测试。
检查两个数字相等、 不等、 大于、 小于、 大于等于和小于等于。
使用关键字
and or 的测试。
测试特定的值是否包含在列表中。
测试特定的值是否未包含在列表中。


name='XY'
if name!='xy':
	print("False")
if name.lower()=='xy':
	print("True")
number1=1
number2=2
if number1<number2:
	print("number1 is less than number2")
if 5<5 and 5>4:
	print("False") 
if 5<5 or 5>4:
	print("True")

chars=['a','b','c']
if 'a' in chars:
	print("Yes,it is.")
if 'd' not in chars:
	print("No,it's not")

5-3 外星人颜色 #1 : 假设在游戏中刚射杀了一个外星人, 请创建一个名为 alien_color 的变量, 并将其设置为 'green' 'yellow' 'red'
编写一条
if 语句, 检查外星人是否是绿色的; 如果是, 就打印一条消息, 指出玩家获得了 5 个点。

编写这个程序的两个版本, 在一个版本中上述测试通过了, 而在另一个版本中未通过(未通过测试时没有输出) 。

alien_color='green'
if alien_color=='green':
	print("Congratulations! you got 5 points")
alien_color='red'
if alien_color=='green':
	print("Congratulations! you got 5 points")


5-4 外星人颜色#2 : 像练习5-3那样设置外星人的颜色, 并编写一个if-else 结构。

如果外星人是绿色的, 就打印一条消息, 指出玩家因射杀该外星人获得了

如果外星人是绿色的, 就打印一条消息, 指出玩家因射杀该外星人获得了5个点。

如果外星人不是绿色的, 就打印一条消息, 指出玩家获得了10个点。

编写这个程序的两个版本, 在一个版本中执行

编写这个程序的两个版本, 在一个版本中执行 if 代码块, 而在另一个版本中执行 else 代码块。

alien_color='green'
if alien_color=='green':
	print("You got five points for shooting the alien.")
else:
	print("You got ten points.")

5-6 人生的不同阶段 : 设置变量 age 的值, 再编写一个 if-elif-else 结构, 根据 age 的值判断处于人生的哪个阶段。
如果一个人的年龄小于
2 岁, 就打印一条消息, 指出他是婴儿。
如果一个人的年龄为
2 (含) ~ 4 岁, 就打印一条消息, 指出他正蹒跚学步。
如果一个人的年龄为
4 (含) ~ 13 岁, 就打印一条消息, 指出他是儿童。
如果一个人的年龄为
13 (含) ~ 20 岁, 就打印一条消息, 指出他是青少年。
如果一个人的年龄为
20 (含) ~ 65 岁, 就打印一条消息, 指出他是成年人。

如果一个人的年龄超过65(含) 岁, 就打印一条消息, 指出他是老年人。

age=19
if age<2:
	print("You are a baby")
elif age>=2 and age<4:
	print("You are a toddler.")
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.")
elif age>=65:
	print("You are old")

5-8 以特殊方式跟管理员打招呼 : 创建一个至少包含 5 个用户名的列表, 且其中一个用户名为 'admin' 。 想象你要编写代码, 在每位用户登录网站后都打印一条问候消息。 遍历用户名列表, 并向每位用户打印一条问候消息。
如果用户名为
'admin' , 就打印一条特殊的问候消息, 如 “Hello admin, would you like to see a status report?”
否则, 打印一条普通的问候消息, 如
“Hello Eric, thank you for logging in again”

users=['admin','xy','wzy','ybh','wyz']
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")

5-11 序数 : 序数表示位置, 如 1st 2nd 。 大多数序数都以 th 结尾, 只有 1 2 3 例外。在一个列表中存储数字 1~9
遍历这个列表。在循环中使用一个
if-elif-else 结构, 以打印每个数字对应的序数。 输出内容应为 1st 2nd 3rd 4th 5th 6th 7th 8th 9th , 但每个序数都独占一行。
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")





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值