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.
  • 相关阅读:
    正则 匹配 HTML 标签
    webpack 打包图片 能否提高加载速度
    禁止手机浏览器左右滑屏 后退 前进
    Objective-C--@property,@synthesize关键字介绍
    ios--绘图介绍
    iOS--为视图添加阴影
    iOS--iOS7摄像头识别二维码功能
    iOS--日历事件的获取和添加
    iOS-#ifdef DEBUG代码块介绍
    如何将你的程序打包成ipa
  • 原文地址:https://www.cnblogs.com/cathycheng/p/11176894.html
Copyright © 2011-2022 走看看