学习笔记4(第5章:if语句)

1、在Python中检查是否相等时是区分大小写的。

2、检查多个条件可以使用关键字andor。  检查特定值是否包含在列表中,可使用关键字in。  检查特定值是否不包含在列表中,可以使用关键字 not in

3、布尔表达式的结果要么是True,要么是False。

4、用for循环运行前检查列表是否为空。

requested_toppings = []
if requested_toppings:
    for requested_topping in requested_toppings:
        print("Adding " + requested_topping + ".")
    print("
Finished making your pizza!")
else:
    print("Are you sure you want a plain pizza?")

这个列表为空,因此输出结果为:

Are you sure you want a plain pizza?

5、p79 5-10 检查用户名,确保每一个用户名的独一无二性。(确保比较时不区分大小写,如果'John’已被使用,应拒绝用户名‘JOHN’)

current_users = ['andy','AMY','Luu','jier','juzi']
new_users = ['Amy','luyan','xiao','luu','pong']

current_users_lower = [user.lower() for user in current_users]

#也可以使用下面的方法
# current_users_lower = [] # for user in current_users: # current_users_lower.append(user.lower()) for new_user in new_users: if new_user.lower() in current_users_lower: print("Sorry " + new_user + ", that name is taken.") else: print("Great, " + new_user + " is still available.")

运行结果:

Sorry Amy, that name is taken.
Great, luyan is still available.
Great, xiao is still available.
Sorry luu, that name is taken.
Great, pong is still available.
原文地址:https://www.cnblogs.com/cathycheng/p/11176894.html