Python习题——2018-03-19作业

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

输出:

He is an adult.


5-7 喜欢的水果:创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if语句,检查列表中是否包含特定的水果。

  • 将该列表命名为favorite_fruits,并在其中包含三种水果。
  • 编写5条if语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如"You really like bananas!"。
favorite_fruits = ['banana', 'pitaya', 'mango']
if 'apple' in favorite_fruits:
    print("You really like apple!")
if 'banana' in favorite_fruits:
    print("You really like banana!")
if 'orange' in favorite_fruits:
    print("You really like orange!")
if 'pear' in favorite_fruits:
    print("You really like pear!")
if 'mango' in favorite_fruits:
    print("You really like mango!")

输出:

You really like banana!
You really like mango!


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!"。
  • 删除列表中的所有用户名,确定将打印正确的消息。
users = ['admin', 'Eric', 'Jack', 'Rose', 'Tom']
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.')
if not users:
    print('We need to find some users!')
del users[:]
if not users:
    print('We need to find some users!')

输出:

Hello admin, would you like to see a status report?
Hello Eric, thank you for logging in again.
Hello Jack, thank you for logging in again.
Hello Rose, thank you for logging in again.
Hello Tom, thank you for logging in again.
We need to find some users!


5-10 检查用户名:按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。

  • 创建一个至少包含5个用户名的列表,并将其命名为current_users。
  • 再创建一个包含5个用户名的列表,将其命名为new_users,并确保其中有一两个用户名也包含在列表current_users中。
  • 遍历列表new_users,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名未被使用。
  • 确保比较时不区分大消息;换句话说,如果用户名'John'已被使用,应拒绝用户名'JOHN'。
current_users = ['Siri', 'Cortana', 'XiaoIce', 'Alexa', 'XiaoAI']
new_users = ['Siri', 'Tom', 'Jerry', 'Me', 'XIAOICE']
current_users_lower = [current_user.lower() for current_user in current_users]
for new_user in new_users:
    if new_user.lower() in current_users_lower:
        print('Please enter another username.')
    else:
        print('This username is not used.')

输出:

Please enter another username.
This username is not used.
This username is not used.
This username is not used.
Please enter another username.

猜你喜欢

转载自blog.csdn.net/Draymond_666/article/details/79614341