5-2 略app
5-3 外星人颜色#1 : 假设在游戏中刚射杀了一个外星人, 请建立一个名为alien_color 的变量, 并将其设置为' green' 、 ' yellow' 或' red' 。
编写一条if 语句, 检查外星人是不是绿色的; 若是是, 就打印一条消息, 指出玩家得到了5个点。
编写这个程序的两个版本, 在一个版本中上述测试经过了, 而在另外一个版本中未经过(未经过测试时没有输出) 。
5-4 外星人颜色#2 : 像练习 5-3那样设置外星人的颜色, 并编写一个if-else 结构。
若是外星人是绿色的, 就打印一条消息, 指出玩家因射杀该外星人得到了5个点。
若是外星人不是绿色的, 就打印一条消息, 指出玩家得到了10个点。
编写这个程序的两个版本, 在一个版本中执行if 代码块, 而在另外一个版本中执行else 代码块。
5-5 外星人颜色#3 : 将练习 5-4中的if-else 结构改成if-elif-else 结构。
若是外星人是绿色的, 就打印一条消息, 指出玩家得到了5个点。
若是外星人是黄色的, 就打印一条消息, 指出玩家得到了10个点。
若是外星人是红色的, 就打印一条消息, 指出玩家得到了15个点。
编写这个程序的三个版本, 它们分别在外星人为绿色、 黄色和红色时打印一条消息。
5-6人生的不一样阶段 : 设置变量age 的值, 再编写一个if-elif-else 结构, 根据age 的值判断处于人生的哪一个阶段。
若是一我的的年龄小于2岁, 就打印一条消息, 指出他是婴儿。
若是一我的的年龄为2(含) ~4岁, 就打印一条消息, 指出他正蹒跚学步。
若是一我的的年龄为4(含) ~13岁, 就打印一条消息, 指出他是儿童测试
若是一我的的年龄为13(含) ~20岁, 就打印一条消息, 指出他是青少年。
若是一我的的年龄为20(含) ~65岁, 就打印一条消息, 指出他是成年人。
若是一我的的年龄超过65(含) 岁, 就打印一条消息, 指出他是老年人。
5-7 喜欢的水果 : 建立一个列表, 其中包含你喜欢的水果, 再编写一系列独立的if 语句, 检查列表中是否包含特定的水果。
将该列表命名为favorite_fruits , 并在其中包含三种水果。
编写5条if 语句, 每条都检查某种水果是否包含在列表中, 若是包含在列表中, 就打印一条消息, 如“You really like bananas!”。网站
答:5-3ui
#1
alien_color = [ 'green']
if 'green' in alien_color :
print("Player have 5 points!")3d
#2
alien_color = ['red']
if 'green' in alien_color :
print("Player have 5 points!")blog
输出结果:three
答:5-4游戏
#1
alien_color = ['green']
if 'green' in alien_color :
print("Player have 5 points!")
else:
print("Player have 10 points!")
#2
alien_color = [ 'red' ]
if 'green' in alien_color :
print("Player have 5 points!")
else:
print("Player have 10 points!")it
输出结果:登录
答 :5-5
#1
alien_color = ['green','yellow','red'] #其余两个版本就是将green改一下
if 'green' in alien_color :
print("Player have 5 points!")
elif 'yellow' in alien_color :
print("Player have 10 points!")
elif 'red' in alien_color :
print("Player have 15 points!")
输出结果:
答:5-6
age = 20
if age < 2 :
print("He is a baby")
if age < 4:
print("He is toddler")
if age < 13:
print("He is He is a child")
if age < 20:
print("He is a teenager")
if age < 65:
print("He is an adult")
if age > 65:
print("He is the elderly")
输出结果:
答:5-7
favorite_fruits =['tomato','banana','apple','pear','peach']
if 'tomato' in favorite_fruits:
print("you really like tomato")
if 'banana' in favorite_fruits:
print("you really like banana")
if 'apple' in favorite_fruits:
print("you really like apple")
if 'pear' in favorite_fruits:
print("you really like pear")
if 'peach' in favorite_fruits:
print("you really like peach")
输出结果:
5-8 以特殊方式跟管理员打招呼 : 建立一个至少包含5个用户名的列表, 且其中一个用户名为' admin' 。 想象你要编写代码, 在每位用户登陆网站后都打印一条问
候消息。 遍历用户名列表, 并向每位用户打印一条问候消息。
若是用户名为' admin' , 就打印一条特殊的问候消息, 如“Hello admin, would you like to see a status report?”。
不然, 打印一条普通的问候消息, 如“Hello Eric, thank you for logging in again”。
5-9 处理没有用户的情形 : 在为完成练习 5-8编写的程序中, 添加一条if 语句, 检查用户名列表是否为空。
若是为空, 就打印消息“We need to find some users!”。
删除列表中的全部用户名, 肯定将打印正确的消息。
5-10 检查用户名 : 按下面的说明编写一个程序, 模拟网站确保每位用户的用户名都独一无二的方式。
建立一个至少包含5个用户名的列表, 并将其命名为current_users 。
再建立一个包含5个用户名的列表, 将其命名为new_users , 并确保其中有一两个用户名也包含在列表current_users 中。
遍历列表new_users , 对于其中的每一个用户名, 都检查它是否已被使用。 若是是这样, 就打印一条消息, 指出须要输入别的用户名; 不然, 打印一条消息, 指
出这个用户名未被使用。
确保比较时不区分大消息; 换句话说, 若是用户名' John' 已被使用, 应拒绝用户名' JOHN' 。
5-11 序数 : 序数表示位置, 如1st和2nd。 大多数序数都以th结尾, 只有一、 2和3例外。
在一个列表中存储数字1~9。
遍历这个列表。
在循环中使用一个if-elif-else 结构, 以打印每一个数字对应的序数。 输出内容应为1st 、 2 nd 、 3rd 、 4 th 、 5th 、 6th 、 7 th 、 8th 和9th , 但每一个序
数都独占一行。
答:5-8
users = ['admin','mumu','lin','mu']
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-9
users = [ ]
if users:
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.')
else:
print("We need to find some users!")
输出结果:
答:5-10
current_users = ['one','two','three','four','five']
new_users = ['one','ttwo','tthree','ffour','five','six']
for new_user in new_users:
if new_user in current_users:
print("'"+ new_user + "'"+ ", " + "It is already occupied, please enter another username")
else:
print("Username is not occupied")
输出结果:
答:5-11
如今作不出来,之后在补上