zoukankan      html  css  js  c++  java
  • 学习笔记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.
  • 相关阅读:
    BZOJ 1854 [Scoi2010]游戏
    【模板】二分图匹配-匈牙利算法
    BZOJ 1432 [ZJOI2009]Function
    BZOJ 1192 [HNOI2006]鬼谷子的钱袋
    BZOJ 1088 [SCOI2005]扫雷Mine
    BZOJ 1047 [HAOI2007]理想的正方形
    BZOJ 1034 [ZJOI2008]泡泡堂BNB
    BZOJ 1022 [SHOI2008]小约翰的游戏John
    LOJ 6278 数列分块入门2
    【BZOJ 1003】[ZJOI2006]物流运输(Dijkstra+DP)
  • 原文地址:https://www.cnblogs.com/cathycheng/p/11176894.html
Copyright © 2011-2022 走看看