5.1编写一系列条件测试;将每个测试以及你对其结果的预测和实际结果都打印出来。你编写的代码应类似于下面这样:

car = 'subaru'
print("Is car == 'subaru'? I predict True.") print(car == 'subaru') print("\nIs car == 'audi'? I predict False.") print(car == 'audi')
health = "great"
print("Is health == 'great'? I predict True.")
print(car == 'great')
print("\nIs health == 'bad'? I predict False.")
print(car == 'bad')

5.2你并非只能创建10个测试。如果你想尝试做更多的比较,可再编写一些测试,并将它们加入到conditional_tests.py中。对于下面列出的各种测试,至少编写一个结果为True 和False 的测试

  • 检查两个字符串相等和不等。
  • 使用函数lower() 的测试。
  • 检查两个数字相等、不等、大于、小于、大于等于和小于等于。
  • 使用关键字and 和or 的测试。
  • 测试特定的值是否包含在列表中。
  • 测试特定的值是否未包含在列表中。
car = "Audi"
print("Is car =='BWM' I predict False.")
print(car == 'BWM')
print(car == 'Audi')
print("Is car =='audi'",(car.lower()=='audi'))
numbers = 24
print("numbers >= 20:")
print(numbers >= 20)
print("numbers <= 20:")
print(numbers <= 20)
print("numbers >= 20:")
print(numbers >= 20)
print("numbers < 20:")
print(numbers < 20)
print(numbers <= 25 and numbers >= 24)
print(numbers >= 25 or numbers <= 23)
number_values= [10,23,54,54]
print(10 in number_values)
print(11 not in number_values)

5.3设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为'green' 、'yellow' 或'red' 。

  • 编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。
  • 编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。
alien_colors = ['green','yellow','red']
alien_color =alien_colors[0]
if alien_color == 'green':
print("you just earned 5 points") alien_color =alien_colors[1]
if alien_color == 'green':
print("you just earned 5 points")
else:
print("you are not earned 5 points")

5.4 像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。

  • 如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点
  • 。 如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。
  • 编写这个程序的两个版本,在一个版本中执行if 代码块,而在另一个版本中执行else 代码块。
alien_colors = ['green','yellow','red']
alien_color =alien_colors[0]
if alien_color == 'green':
print("you just earned 5 points")
elif alien_color =='yellow':
print("you just earned 10 points")
elif alien_color == 'red':
print("you just earned 15 points") alien_color =alien_colors[1]
if alien_color == 'green':
print("you just earned 5 points")
elif alien_color =='yellow':
print("you just earned 10 points")
elif alien_color == 'red':
print("you just earned 15 points") alien_color =alien_colors[2]
if alien_color == 'green':
print("you just earned 5 points")
elif alien_color =='yellow':
print("you just earned 10 points")
elif alien_color == 'red':
print("you just earned 15 points")

5.5 将练习5-4中的if-else 结构改为if-elif-else 结构。

  • 如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。
  • 如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。
  • 如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。
  • 编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。
alien_colors = ['green','yellow','red']
alien_color =alien_colors[0]
if alien_color == 'green':
print("you just earned 5 points")
elif alien_color =='yellow':
print("you just earned 10 points")
elif alien_color == 'red':
print("you just earned 15 points") alien_color =alien_colors[1]
if alien_color == 'green':
print("you just earned 5 points")
elif alien_color =='yellow':
print("you just earned 10 points")
elif alien_color == 'red':
print("you just earned 15 points") alien_color =alien_colors[2]
if alien_color == 'green':
print("you just earned 5 points")
elif alien_color =='yellow':
print("you just earned 10 points")
elif alien_color == 'red':
print("you just earned 15 points")

5.6 设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。

  • 如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。
  • 如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。
  • 如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。
  • 如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。
  • 如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。
  • 如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。
age = 24
if age < 2:
print("the person is a baby.")
if age < 4:
print("the person is a toddler.")
if age < 13:
print("the person is a kid.")
if age < 20:
print("the person is a teenager.")
if age < 65:
print("the person is an adult.")
if age >= 65:
print("the person is an elder.")

5.7 创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if 语句,检查列表中是否包含特定的水果。

  • 将该列表命名为favorite_fruits ,并在其中包含三种水果。
  • 编写5条if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如“You really like bananas!”。
fruits = ['cucumber','apple','strawberry','banana']
if 'apple' in fruits:
print("You really like apple.")
if 'banana' in fruits:
print("You really like banana.")
if 'strawberry' in fruits:
print("You really like strawberry.")
if 'cucumber' in fruits:
print("You really like cucumber.")
if 'pear' in fruits:
print("You really like pear.")

5.8 创建一个至少包含5个用户名的列表,且其中一个用户名为'admin' 。想象你要编写代码,在每位用户登录网站后都打印一条问 候消息。遍历用户名列表,并向每位用户打印一条问候消息。

  • 如果用户名为'admin' ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。
  • 否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。
usernames = ["joey","chandler","monica","ross","rachel","phoebe","admin"]
for name in usernames:
if name == 'admin':
print(f"Hello,{name}, would you like to see a status report.")
else:
print(f"Hello {name},thank you fo logging in again.")

5.9 在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。

  • 如果为空,就打印消息“We need to find some users!”。

  • 删除列表中的所有用户名,确定将打印正确的消息。

usernames = []
if not usernames:
print("We need to find some users!")

5.10 按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。

  • 创建一个至少包含5个用户名的列表,并将其命名为current_users 。 再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。

  • 遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指 出这个用户名未被使用。

  • 确保比较时不区分大消息;换句话说,如果用户名'John' 已被使用,应拒绝用户名'JOHN'

current_users = ["joey","chandler","monica","ross","rachel","phoebe","admin"]
new_users =["Joey","admin","susan","tom"]
for user in new_users:
if user.lower() in current_users:
print(f"you need a new username.{user}")
else:
print(f"the username is available.{user}")

5.11 序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。

  • 在一个列表中存储数字1~9。 遍历这个列表。

  • 在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序 数都独占一行。

numbers =[1,2,3,4,5,6,7,8,9,10]
for nubmer in numbers:
if nubmer == 1:
print("1st")
elif nubmer == 2:
print("2nd")
elif nubmer == 3:
print("3rd")
else:
print(f"{nubmer}th")

PythonCrashCourse 第五章习题的更多相关文章

  1. 《学习Opencv》第五章 习题6

    这是第五章 习题5.6的结合版,其中实现了摄像头抓拍功能,能够成功运行. #include "stdafx.h" #include "cv.h" #includ ...

  2. 统计学习导论:基于R应用——第五章习题

    第五章习题 1. 我们主要用到下面三个公式: 根据上述公式,我们将式子化简为 对求导即可得到得到公式5-6. 2. (a) 1 - 1/n (b) 自助法是有有放回的,所以第二个的概率还是1 - 1/ ...

  3. PythonCrashCourse 第三章习题

    PythonCrashCourse 第三章习题 3.1 将一些朋友的姓名存储在一个列表中,并将其命名为names.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来 names = ['lih ...

  4. PythonCrashCourse 第四章习题

    Python 从入门到实践第四章习题 4.1想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来 修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅 ...

  5. PythonCrashCourse 第七章习题

    编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如"Let me see if I can find you a Subaru" car =input("Wha ...

  6. C和指针 第十五章 习题

    15.8 十六进制倾印码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  7. C和指针 第五章 习题

    下列输出的值: #include <stdio.h> int func(){ static int count = 1; return ++count; } int main() { in ...

  8. [家里蹲大学数学杂志]第269期韩青编《A Basic Course in Partial Differential Equations》 前五章习题解答

    1.Introduction 2.First-order Differential Equations Exercise2.1. Find solutons of the following inti ...

  9. python核心编程-第五章-习题

    1.长整型表示数的范围比整型更大.在python中,整型.长整型趋于统一,普通用户不用特别关注两者区别,仅当需引用C语言时需要特别注意. 2.操作符 (a) def product(x,y): ret ...

随机推荐

  1. 使用jwt进行token认证

    简单说明:最近在搞权限这一块的东西,需要用到jwt进行token认证,才有了如下的demo演示   具体细节可以看gitbug,噗,不是bug是hub  github地址:https://github ...

  2. vue学习(十五) 过滤器简单实用

    vue过滤器: 概念:vue.js允许你自定义过滤器可被用作一些常见文本的格式化.过滤器可以用在两个地方:插值表达式   v-bind表达式  由管道符指示 //过滤器调用时候的格式 {{ name ...

  3. hadoop2.7.3+spark2.0.1+scala2.11.8集群部署

    一.环境 4.用户 hadoop 5.目录规划 /home/hadoop/app    #程序目录 /home/hadoop/data  #数据目录     #打开文件的最大数 vi /etc/sec ...

  4. 解惑,什么是data-attribute ?

    在接触 Web前端开发的一段时间,有时会去看Google或者百度的源代码,有某些地方定义了 data-key ,这种语法 但是如果你直接去 Google data-key 或 data-item 可能 ...

  5. 【评价指标】详解F1-score与多分类MacroF1&MicroF1

    文章来自:一个宝藏微信公众号[机器学习炼丹术] 基本概念 首先,要背住的几个概念就是:accuracy,precision,recal, TP,FP,TN,FN TP:true positive.预测 ...

  6. matplotlib绘制子图

    fig,subs = plt.subplots(2,2) subs[0][0].plot(data_math_C1) subs[0][0].set_title('C_1 曲线') subs[0][1] ...

  7. Python os.rmdir() 方法

    概述 os.rmdir() 方法用于删除指定路径的目录.仅当这文件夹是空的才可以, 否则, 抛出OSError.高佣联盟 www.cgewang.com 语法 rmdir()方法语法格式如下: os. ...

  8. PHP代码混淆与加密——php screw plus

    php是一个开源的.广受欢迎的语言,php应用常常是以代码明文的发式发布,但是有时候对于发布的代码我们想要进行保护,需要对php代码进行混淆与加密,让增加破解者的逆向难度,从而达到保护自己成果的目的. ...

  9. 利用mvc模式,实现用户的注册

    实现功能:利用mvc模式,实现用户的登陆注册功能 1.程序的框架结构 2个包,bean,以及servlet 3个jsp页面,注册页面,注册成功页面,注册失败页面 mysql驱动 2.编程思想 通过js ...

  10. 如何在Spring异步调用中传递上下文

    以下文章来源于aoho求索 ,作者aoho 1. 什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步 ...