python从入门到实践第五章的练习题作业

'''
5-1 条件测试:编写一系列条件测试;将每个测试以及你对其结果的预测和实际结
果都打印出来。你编写的代码应类似于下面这样:
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
 详细研究实际结果,直到你明白了它为何为 True 或 False。
 创建至少 10 个测试,且其中结果分别为 True 和 False 的测试都至少有 5 个。
'''
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')

print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')

ele = (1,2)
print("\nIs ele == (1,2)? I predict True.")
print(ele == (1,2))

print("\nIs ele == (2,3)? I predict False.")
print(car == (2,3))


fruit = 'watermelon'
print("\nIs fruit == ''? I predict True.")
print(fruit == 'watermelon')

print("\nIs fruit == 'apple'? I predict False.")
print(fruit == 'apple')


passed = True
print("\nIs passed == 'True'? I predict True.")
print(passed== True)

print("\nIs passed == 'False'? I predict False.")
print(passed == False)

grade = 98
print("\nIs grade == 98? I predict True.")
print(grade == 98)

print("\nIs car == 95? I predict False.")
print(grade == 95)

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

print('Is car1 == car2?')
print(car1 == car2)
print('Is car1 != car2?')
print(car1 != car2)


fruit1 = 'Watermelon'
fruit2 = 'watermelon'
print('Is fruit1 == fruit2?')
print(fruit1 == fruit2)
print('Is fruit1 == fruit2?(Use lower()function)')
print(fruit1.lower()==fruit2.lower())

num1 = 99
num2 = 199
print('Is num1 == num2?')
print(num1 == num2)
print('Is num1 != num2?')
print(num1 != num2)
print('Is num1 > num2?')
print(num1 > num2)
print('Is num1<num2?')
print(num1 < num2)
print('Is num1 >= num2?')
print(num1 >= num2)
print('Is num1 <= num2?')
print(num1 <= num2)

flag1 = True
flag2 = False
print('flag1 and flag2 = '+str(flag1 and flag2))
print('flag1 or flag2 = '+str(flag1 or flag2))

cars = ['toyota','benz','dongfeng']
car1 = 'dongfeng'
car2 = 'jili'
flag1 = False
flag2 = False
for value in cars:
	if car1 == value:
		flag1 = True
	if car2 == value:
		flag2 = True
print('Is car1 in cars(list)? '+str(flag1))
print('Is car2 in cars(list)? '+str(flag2))
print('Is car1 not in cars(list)? '+str(not flag1))
print('Is car2 not in cars(list)? '+str(not flag2))

'''
5-3 外星人颜色 #1:假设在游戏中刚射杀了一个外星人,请创建一个名为
alien_color 的变量,并将其设置为'green'、 'yellow'或'red'。
 编写一条 if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出
玩家获得了 5 个点。
 编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中
未通过(未通过测试时没有输出)。

'''

alien_color = ['green','yellow','red']
color1 = 'green'
color2 = 'blue'


if color1 == 'green':
	print('you get five points')


if color2 == 'green':
	print('you get five points')

'''
5-4 外星人颜色#2:像练习 5-3 那样设置外星人的颜色,并编写一个 if-else 结构。
 如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了 5 个
点。
 如果外星人不是绿色的,就打印一条消息,指出玩家获得了 10 个点。
 编写这个程序的两个版本,在一个版本中执行 if 代码块,而在另一个版本中执
行 else 代码块。
'''

alien_color = ['green','yellow','red']
color1 = 'green'
color2 = 'blue'

if color1 == 'green':
	print('you get five points')
else:
	print('you get ten points')


if color2 == 'green':
	print('you get five points')
else:
	print('you get ten points')

'''
5-5 外星人颜色#3:将练习 5-4 中的 if-else 结构改为 if-elif-else 结构。
 如果外星人是绿色的,就打印一条消息,指出玩家获得了 5 个点。
 如果外星人是黄色的,就打印一条消息,指出玩家获得了 10 个点。
 如果外星人是红色的,就打印一条消息,指出玩家获得了 15 个点。
 编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条
消息。
'''
alien_color = ['green','yellow','red']
color1 = 'green'
color2 = 'yellow'
color3 = 'red'

if color1 =='green':
	print('you get five points')
elif color1 == 'yellow':
	print('you get ten points')
else:
	print('you get 15 points')

if color2 =='green':
	print('you get five points')
elif color2 == 'yellow':
	print('you get ten points')
else:
	print('you get 15 points')

if color3 =='green':
	print('you get five points')
elif color3 == 'yellow':
	print('you get ten points')
else:
	print('you get 15 points')

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

ages  = input('print an age:')
age  = int(ages)
if age < 2:
	print('A baby')
elif age >= 2 and age < 4:
	print('learn to walk')
elif age >=4 and age <13:
	print('A child')
elif age >=13 and age<20:
	print('A teenager')
elif age >= 20 and age <65:
	print('An adult')
elif age >=65:
	print('An old man')

'''
5-7 喜欢的水果:创建一个列表,其中包含你喜欢的水果,再编写一系列独立的 if
语句,检查列表中是否包含特定的水果。
 将该列表命名为 favorite_fruits,并在其中包含三种水果。
 编写 5 条 if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,
就打印一条消息,如“You really like bananas!”。
'''

fruits = ['bananas','apples','pears','peaches','mango']

if 'bananas' in fruits:
	print('bananas in fruits')

if 'lemon' in fruits:
	print('lemon in fruits')

favorite_fruits = ['bananas','peaches','pears']
for fruit in fruits:
	if fruit in favorite_fruits:
		print('You really like '+ fruit+' !')

'''
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','cc','lily','lucy','maro']
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')

'''
5-9 处理没有用户的情形:在为完成练习 5-8 编写的程序中,添加一条 if 语句,检
查用户名列表是否为空。
 如果为空,就打印消息“We need to find some users!”。
 删除列表中的所有用户名,确定将打印正确的消息。
'''
users = ['admin','cc','lily','lucy','maro']
for user in users:
	if not users:
		print ('We need to find some 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')

while users:
	users.pop()

if not users:
		print ('We need to find some users!')

'''

5-10 检查用户名:按下面的说明编写一个程序,模拟网站确保每位用户的用户名
都独一无二的方式。
 创建一个至少包含 5 个用户名的列表,并将其命名为 current_users。
 再创建一个包含 5 个用户名的列表,将其命名为 new_users,并确保其中有一两
个用户名也包含在列表 current_users 中。
 遍历列表 new_users,对于其中的每个用户名,都检查它是否已被使用。如果是
这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指
出这个用户名未被使用。
 确保比较时不区分大消息;换句话说,如果用户名'John'已被使用,应拒绝用户
名'JOHN'
'''

current_users = ['ctrits','cc','lily','lucy','maro']
new_users = ['dragon','ai','suyuri','sukura','LIly']
for x in new_users:
	if x.lower() in current_users:
		print('you need to use another name, since this name has been used!')
	else:
		print(x +' has not been used!')


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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值