高级编程技术 week3 第五章

5-1 条件测试:编写一系列条件测试;将每个测试以及你对其结果的预测和实际结果都打印出来。你编写的代码应类似于下面这样:

car = 'subaru'

print("Is car == 'subaru'? I predict True.")

print(car == 'subaru')

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

print(car == 'audi')

    1)详细研究实际结果,直到你明白了它为何为True或False。 

    2)创建至少10个测试,且其中结果分别为True 和False 的测试都至少有5个。 

car = 'Toyota'
print("Is car == 'Toyota'? I predict True.")
print(car == 'Toyota')
print("Is car == 'Benz'? I predict False.")
print(car == 'Benz')

number = '18'
print("\nIs number >= '18'? I predict True.")
print(number >= '18')
print("Is number >= '20'? I predict False.")
print(number >= '20')

persons = ['Tom', 'Mike', 'Jack']
print("\nIs 'Jack' in persons? I predict True.")
print('Jack' in persons)
print("Is 'Mary' in persons? I predict False.")
print('Mary' in persons)

Flag = True
print("\nIs Flag == True? I predict True.")
print(Flag == True)
print("Is Flag == False? I predict False.")
print(Flag == False)

value = 2
print("\nIs value * 2 == 4? I predict True.")
print(value * 2 == 4)
print("Is value * 2 == 8? I predict False.")
print(value * 2 == 8)

5-3 外星人颜色#1:假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color的变量,并将其设置为'green'、'yellow'或'red'。

    1)编写一条if语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。

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

print("Test 1")
alien_color ='green'  
if alien_color =='green':  
    print('you get five points')  
print("\nTest 2")
alien_color = 'yellow'  
if alien_color == 'green':  
    print('you get five points') 

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

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

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

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

print("Test 1")
alien_color ='green'  
if alien_color =='green':  
    print('you get five points')
else:
    print('you get ten points')
print("\nTest 2")
alien_color = 'yellow'  
if alien_color == 'green':  
    print('you get five points')
else:
    print('you get ten points')

5-5 外星人颜色#3:将练习5-4中的if-else结构改为if-elif-else结构。

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

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

    3)如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。

    编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。

print("Test 1")
alien_color ='green'  
if alien_color =='green':  
    print('you get five points')
elif alien_color =='yellow':
    print('you get ten points')
else:
    print('you get fifteen points')
    
print("\nTest 2")
alien_color ='yellow'  
if alien_color =='green':  
    print('you get five points')
elif alien_color =='yellow':
    print('you get ten points')
else:
    print('you get fifteen points')
    
print("\nTest 3")
alien_color ='red'  
if alien_color =='green':  
    print('you get five points')
elif alien_color =='yellow':
    print('you get ten points')
else:
    print('you get fifteen points')

5-6 人生的不同阶段:设置变量age的值,再编写一个if-elif-else结构,根据age的值判断处于人生的哪个阶段。

    1)如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。

    2)如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。

    3)如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。

    4)如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。

    5)如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。

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

age = 25  
if age < 2:  
    print('He is a baby')
elif age >= 2 and age < 4:
    print('He is learning walking')
elif age >= 4 and age < 13:
    print('He is a child')
elif age >= 13 and age < 20:
    print('He is a teen')
elif age >= 20 and age < 65:
    print('He is an adult')
else:
    print('he is an old man')

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

    1)将该列表命名为favorite_fruits,并在其中包含三种水果。

    2)编写5条if语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如“You really like bananas!”。 

favorite_fruits = ["apples", "pears", "mangos"]
if "apples" in favorite_fruits:
    print("You really like apples!")
if "watermelons" in favorite_fruits:
    print("You really like watermelons!")
if "pears" in favorite_fruits:
    print("You really like pears!")
if "mangos" in favorite_fruits:
    print("You really like mangos!")
if "oranges" in favorite_fruits:
    print("You really like oranges!")

5-8 以特殊方式跟管理员打招呼:创建一个至少包含5个用户名的列表,且其中一个用户名为'admin'。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。

    1)如果用户名为'admin',就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。

    2)否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。

users = ["Tom", "admin", "Jack", "Stephen", "Mary"]
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语句,检查用户名列表是否为空。

    1)如果为空,就打印消息“We need to find some users!”。

    2)删除列表中的所有用户名,确定将打印正确的消息。

users = ["Tom", "admin", "Jack", "Stephen", "Mary"]
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!")

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

    1)创建一个至少包含5个用户名的列表,并将其命名为current_users。

    2)再创建一个包含5个用户名的列表,将其命名为new_users,并确保其中有一两个用户名也包含在列表current_users中。

    3)遍历列表new_users,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名未被使用。 确保比较时不区分大消息;换句话说,如果用户名'John'已被使用,应拒绝用户名'JOHN' 。

current_users = ["Tom", "Mike", "Jack", "Stephen", "Mary"]
new_users = ["Jack", "Lily", "June", "Tom", "Helen"]
for new_user in new_users:
    if new_user.upper() in [current_user.upper() for current_user in current_users]:
        print(new_user + " is used, please use another name")
    else:
        print(new_user + " is not used")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值