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.
  • 相关阅读:
    #Leetcode# 876. Middle of the Linked List
    #Leetcode# 237. Delete Node in a Linked List
    #Leetcode# 234. Palindrome Linked List
    #Leetcode# 149. Max Points on a Line
    #Leetcode# 152. Maximum Product Subarray
    #Leetcode# 228. Summary Ranges
    #Leetcode# 227. Basic Calculator II
    PAT 1089 狼人杀-简单版
    linux——web安全之sql注入【预习阶段】
    linux——攻防技术介绍|主动攻击|被动攻击
  • 原文地址:https://www.cnblogs.com/cathycheng/p/11176894.html
Copyright © 2011-2022 走看看