zoukankan      html  css  js  c++  java
  • python编程:从入门到实践----第五章:if语句>练习

    5-1 条件测试 :编写一系列条件测试;将每个测试以及你对其结果的预测和实际结果都打印出来。
    a. 详细研究实际结果,直到你明白了它为何为True 或False
    b. 创建至少2个测试,且其中结果分别为True 和False 的测试都至少有2个。
    food = 'meat'
    print("Is food == 'meat'? I predict True")
    print(food =='meat')
    print("
    Is food == 'apple'? I predict False")
    print(food =='apple')
    
    fruit = 'banana'
    print("
    Is fruit == 'banana'? I predict True")
    print(fruit =='banana')
    print("
    Is fruit == 'apple'? I predict False")
    print(fruit =='apple')
    
    #输出结果:
    Is food == 'meat'? I predict True
    True
    
    Is food == 'apple'? I predict False
    False
    
    Is fruit == 'banana'? I predict True
    True
    
    Is fruit == 'apple'? I predict False
    False
    

      

    5-2 更多的条件测试:你并非只能创建10个测试。如果你想尝试做更多的比较,可再编写一些测试,并将它们加入到conditional_tests.py中。对于下面列出的各种测试,至少编写一个结果为True 和False 的测试。
    a. 检查两个字符串相等和不等。
    b. 使用函数lower() 的测试。
    c. 检查两个数字相等、不等、大于、小于、大于等于和小于等于。
    d. 使用关键字and 和or 的测试。
    e. 测试特定的值是否包含在列表中。
    f. 测试特定的值是否未包含在列表中。
    '''
    i1='apple'
    i2='Apple'
    print(i1 == i2)
    print(i1 != i2)
    print(i1 == i2.lower())
    '''
    '''
    i3=20
    i4=30
    print(i3 == i4)
    print(i3 != i4)
    print(i3 > i4)
    print(i3 >= i4)
    print(i3 < i4)
    print(i3 <= i4)
    print(i3<=20 and i4<=20)
    print(i3<=20 or i4<=20)
    '''
    j=['apple','banan','orange']
    j1 = 'apple'
    print(j1 in j)
    print(j1 not in j)
    

      

     
    5-3 外星人颜色#1 :假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为'green' 、'yellow' 或'red' 。
    a.编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。
    b.编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。
    '''
    alien_color = 'green'
    if alien_color =='green':
        print("You get 5 points")
    #输出结果:You get 5 points
    '''
    alien_color = 'green'
    if alien_color =='orange':
        print("You get 5 points")
    #输出结果:空
    

      

     5-4 外星人颜色#2 :像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。
    a.如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点。
    b.如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。
    c. 编写这个程序的两个版本,在一个版本中执行if 代码块,而在另一个版本中执行else 代码块。
    alien_color = 'green'
    if alien_color =='green':
        print("You shoot alien. You get 5 points")
    '''
    else:
        print("You get 10 points")
    '''
    

      

     5-5 外星人颜色#3 :将练习5-4中的if-else 结构改为if-elif-else 结构。
    a. 如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。
    b. 如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。
    c. 如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。
    d. 编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。
    alien_color = 'green'
    if alien_color =='green':
        print("You got 5 points")
    elif alien_color =='yellow':
        print("You got 10 points")
    else:
        print("You got 15 points")
    
    #输出结果:You got 5 points
    备注:黄色和红色打印的消息的代码省略,只要把变量中的值改为黄色和红色即可。
    

      

     5-6 人生的不同阶段:设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。
    a.如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。
    b.如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。
    c.如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。
    d.如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。
    e.如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。
    f.如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。
    age = 64
    if age <2:
        print("He is baby")
    elif  2<=age<4 :
        print("He just learn to walk")
    elif 4<=age <13:
        print("He is a child")
    elif 13<=age <20:
        print("He is teenager")
    elif 20<= age <65:
        print("He is a adult")
    else:
        print("He is a elderly")
    
    #输出结果:He is a adult
    

      

     5-7 喜欢的水果 :创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if 语句,检查列表中是否包含特定的水果。
    a.将该列表命名为favorite_fruits ,并在其中包含三种水果。
    b.编写5条if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如“You really like bananas!”。
     
    favorite_fruit = ['apple','orange','banana']
    if 'apple' in favorite_fruit:
        print("You really like apple!")
    if 'orange' in favorite_fruit:
        print("You really like orange!")
    if 'banana' in favorite_fruit:
        print("You really like bananas!")
    if 'pear' in favorite_fruit:
        print()
    if 'watermelon' in favorite_fruit:
        print()
    
    #输出结果:
    You really like apple!
    You really like orange!
    You really like bananas!
    
    5-8 以特殊方式跟管理员打招呼 :创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。
    a. 如果用户名为'admin' ,就打印一条特殊的问候消息,如“Hello admin, would you liketo seeastatus report?”。
    b. 否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。
    users=["admin","crystal","James","Lily","Lucy"]
    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.")
    
    #输出结果:
    Hello admin, would you like to see a status report?
    Hello crystal,thank you for logging in again.
    Hello James,thank you for logging in again.
    Hello Lily,thank you for logging in again.
    Hello Lucy,thank you for logging in again.
    

      

     5-9 处理没有用户的情形 :在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。
    a. 如果为空,就打印消息“We need to find some users!”。
    b. 删除列表中的所有用户名,确定将打印正确的消息。
    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!")
    
    #输出结果:
    We need to  find some users!
     
    5-10 检查用户名 :按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
    a.创建一个至少包含5个用户名的列表,并将其命名为current_users 。
    b.再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。
    c.遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名未被使用。
    d.确保比较时不区分大消息;换句话说,如果用户名'John' 已被使用,应拒绝用户名'JOHN'
    current_users =['admin','crystal','James','Lucy','Lily']
    new_users = ['Admin','Crystal','Jim','Candy','lily']
    for new_user in new_users:
        if new_user.lower() in [current_user.lower() for current_user in current_users]:
        #[current_user.lower() for current_user in current_users]  列表不区分大小写,也就是列表解析
            print(new_user + ",Please input other user")
        else:
            print(new_user + ",This user is not used")
    
    #输出结果:
    Admin,Please input other user
    Crystal,Please input other user
    Jim,This user is not used
    Candy,This user is not used
    lily,Please input other user
    

      

    5-11 序数 :序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。
    a.在一个列表中存储数字1~9。
    b. 遍历这个列表。
    c. 在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序
    数都独占一行。
    numbers = [1,2,3,4,5,6,7,8,9]
    for number in numbers:
        if number ==1:
            print("1st")
        elif number == 2:
            print("2nd")
        elif number == 3:
            print("3rd")
        else:
            print(str(number)+"th")
    #输出结果
    1st
    2nd
    3rd
    4th
    5th
    6th
    7th
    8th
    9th
    

      

     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    使用java连接数据库以后显示“ Establishing SSL connection without server's identity verification is not recommended”的警告如何解决
    使用java发送QQ邮件
    编写java的时候出现“编码GBK的不可映射字符”
    java如何调用另一个包里面的类
    postman断言的方法
    postman参数化的方法
    postman批量执行 要给请求加断言,批量执行的时候才会去统计,成功和失败的条数
    python查找字符串 函数find() 用法
    postman请求ajax失败的解决方法
    jmeter 模拟ajax/ https请求 失败的解决方法
  • 原文地址:https://www.cnblogs.com/heiqiuxixi/p/12330141.html
Copyright © 2011-2022 走看看