Quiz 5: Loops and Iterations | Python for Everybody 配套练习_解题记录

news2025/2/21 22:49:14

文章目录

  • 课程简介
      • Quiz 5: Loops and Iterations
    • 单选题(1-10)
    • 编程题
      • Exercise 5.2


课程简介

Python for Everybody 零基础程序设计(Python 入门)

  • This course aims to teach everyone the basics of programming computers using Python. 本课程旨在向所有人传授使用 Python 进行计算机编程的基础知识。
  • We cover the basics of how one constructs a program from a series of simple instructions in Python. 我们介绍了如何通过 Python 中的一系列简单指令构建程序的基础知识。
  • The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. 该课程没有任何先决条件,除了最简单的数学之外,避免了所有内容。任何具有中等计算机经验的人都应该能够掌握本课程中的材料。
  • This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. 本课程将涵盖《Python for Everyday》教科书的第 1-5 章。学生完成本课程后,他们将准备好学习更高级的编程课程。
  • This course covers Python 3.

在这里插入图片描述

coursera

Python for Everybody 零基础程序设计(Python 入门)

Charles Russell Severance
Clinical Professor

在这里插入图片描述

个人主页
Twitter

在这里插入图片描述

University of Michigan


课程资源

coursera原版课程视频
coursera原版视频-中英文精校字幕-B站
Dr. Chuck官方翻录版视频-机器翻译字幕-B站

PY4E-课程配套练习
Dr. Chuck Online - 系列课程开源官网


Quiz 5: Loops and Iterations

We look at how Python repeats statements using looping structures.



单选题(1-10)

  1. What is wrong with this Python loop:
n = 5
while n > 0 :
    print(n)
print('All done')
  • This loop will run forever
  • while is not a Python reserved word
  • The print(‘All done’) statement should be indented four spaces
  • There should be no colon on the while statement
  1. What does the break statement do?
  • Resets the iteration variable to its initial value
  • Jumps to the “top” of the loop and starts the next iteration
  • Exits the currently executing loop
  • Exits the program
  1. What does the continue statement do?
  • Resets the iteration variable to its initial value
  • Jumps to the “top” of the loop and starts the next iteration
  • Exits the currently executing loop
  • Exits the program
  1. What does the following Python program print out?
tot = 0
for i in [5, 4, 3, 2, 1] :
    tot = tot + 1
print(tot)
  • 10
  • 5
  • 15
  • 0
  1. What is the iteration variable in the following Python code:
friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
     print('Happy New Year:',  friend)
print('Done!')
  • Joseph
  • friend
  • Glenn
  • Sally
  1. What is a good description of the following bit of Python code?
zork = 0
for thing in [9, 41, 12, 3, 74, 15] :
    zork = zork + thing
print('After', zork)
  • Count all of the elements in a list
  • Find the smallest item in a list
  • Compute the average of the elements in a list
  • Sum all the elements of a list
  1. What will the following code print out?
smallest_so_far = -1
for the_num in [9, 41, 12, 3, 74, 15] :
   if the_num < smallest_so_far :
      smallest_so_far = the_num
print(smallest_so_far)

Hint: This is a trick question and most would say this code has a bug - so read carefully

  • 74
  • -1
  • 3
  • 42
  1. What is a good statement to describe the is operator as used in the following if statement:
if smallest is None :
     smallest = value
  • matches both type and value
  • Is true if the smallest variable has a value of -1
  • Looks up ‘None’ in the smallest variable if it is a string
  • The if statement is a syntax error
  1. Which reserved word indicates the start of an “indefinite” loop in Python?
  • break
  • while
  • def
  • for
  • indef
  1. How many times will the body of the following loop be executed?
n = 0
while n > 0 :
    print('Lather')
    print('Rinse')
print('Dry off!')
  • 0
  • 1
  • 5
  • This is an infinite loop

编程题

Exercise 5.2

题目:
Write a program that repeatedly prompts a user for integer numbers until the user enters ‘done’.
Once ‘done’ is entered, print out the largest and smallest of the numbers.
If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number.
Enter 7, 2, bob, 10, and 4 and match the output below.

[Desired Output]
Invalid input
Maximum is 10
Minimum is 2

解题代码:

largest = None
smallest = None

while True:
   num = input("Enter a number: ")
   
   if num == "done":
       break
   try:        
       n = int(num)
   except:
       print("Invalid input")
       continue
   else:
       if largest is None:
           largest = n
       elif largest < n:
           largest = n
           
       if smallest is None:
           smallest = n
       elif smallest > n:
           smallest = n

print("Maximum", "is", largest)
print("Minimum", "is", smallest)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/701834.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++语法练习(牛客题库)——练习1

1. 下列程序的运行结果是 1*2 3*4&#xff0c;那么横线处缺失程序可以是&#xff08;&#xff09; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <iostream> using namespace std; class Test{ public: Test(int a, int b) { …

【运维知识进阶篇】zabbix5.0稳定版详解6(zabbix自动化监控:自动发现+自动注册+监控项目主动式)

本篇文章继续给大家介绍zabbix自动化监控&#xff0c;包括zabbix自动注册&#xff0c;zabbix自动发现&#xff0c;将主机添加进服务端之后需要做的监控项目更改为主动式&#xff0c;zabbix说多不多&#xff0c;说少不少&#xff0c;其实远没有监控那么简单&#xff0c;更深层次…

【运维知识进阶篇】zabbix5.0稳定版详解7(zabbix分布式监控:使用场景+功能详解+快速部署+基本使用)

如果你有几百上千台客户端的数据需要上报给zabbix服务端&#xff0c;即便是你做了主动注册&#xff0c;监控项目主动式&#xff0c;那服务端压力还是会很大&#xff0c;所以我们可以考虑zabbix分布式监控。 zabbix proxy可以代替zabbix server收集性能和可用性数据&#xff0c…

【HTTP 协议1】图文详解 HTTP 请求和应答报文

文章目录 前言一、认识 HTTP 协议1, 什么是 HTTP 协议2, HTTP 协议的报文格式 二、HTTP 请求报文1, 认识方法1.1, GET 和 POST 辨析(重点)1.2, 其他方法 2, 认识 URL3, 认识 Header3.1, Host3.2, Content-Length3.3 Content-Type3.4, User-Agent3.5, Referer3.6, Cookie(重点) …

源代码|大屏可视化系统 数据可视化

代码拿来即可用&#xff0c;按照下文步骤配置&#xff0c;傻瓜式教程&#xff0c;几分钟即可搞定。 需要代码源文件&#xff0c;请移步至gzh【李桥桉】&#xff0c;s辛【可视化】。 可视化效果图 运行环境&#xff1a;VScode 文末附《大屏可视化系统》源代码获取方式~ 一、打…

数据结构--顺序栈的实现

数据结构–顺序栈的实现 顺序栈的定义 顺序栈的定义代码实现 #define MaxSize 10 typedef struct {ElemType data[MaxSize]; //静态数组存放栈中元素int top; //栈顶指针 } SqStack;int main() {SqStack S; //声明一个顺序栈(分配空间)//... ...return 0; }一些常见操作 初始…

大数据的金融数据读取及分析(-)

由于考虑商业数据问题&#xff0c;我们用开源数据做演示 一.tushare开源数据 Tushare是一个免费、开源的python财经数据接口包。主要实现对股票等金融数据从数据采集、清洗加工到数据存储的过程&#xff0c;能够为金融分析人员提供快速、整洁、和多样的便于分析的数据&#x…

postgresql_internals-14 学习笔记(七)—— parallel 并行

不完全来自这本书&#xff0c;把查到的和之前的文章重新汇总整理了一把。 一、 核心参数 几个容易弄混的进程和参数&#xff0c;关系图如下 1. max_worker_processes 整个实例可以同时运行的Background workers Processes最大数量默认值为8&#xff0c;设置为0表示禁用并行&…

STM32与树莓派:嵌入式系统开发与教育计算的区别

STM32和树莓派是两种不同的硬件平台&#xff0c;用于不同的应用领域。 STM32&#xff1a;STM32是一系列由STMicroelectronics&#xff08;意法半导体&#xff09;生产的32位ARM Cortex-M微控制器。它们被广泛用于嵌入式系统开发&#xff0c;包括消费电子产品、工业自动化、汽车…

CNN池化总结(最大池化与平均池化)

目录 概念 两种主要池化方式 最大池化 平均池化 尺寸变化过程 池化优点 总结 概念 池化&#xff08;Pooling&#xff09;&#xff0c;用于减小卷积神经网络&#xff08;CNN&#xff09;或其他类型神经网络的特征图&#xff08;Feature Map&#xff09;的尺寸&#xff0…

java 网络教学平台Myeclipse开发mysql数据库web结构jsp编程计算机网页项目

一、源码特点 JSP 网络教学平台 是一套完善的系统源码&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;以及相应配套的设计文档&#xff0c;系统主要采用B/S模式开发。 研究的基本内容是基于Web的网络教学平台&…

【SWAT水文模型】SWAT-CUP参数率定过程问题总结

SWAT-CUP参数率定过程问题总结 Q1 SWAT-CUP中calibrate按钮一直是灰色无法点击?1.1 问题描述1.2 解决办法 Q2 “SWAT”不是内部命令1.1 问题描述1.2 解决办法 参考 Q1 SWAT-CUP中calibrate按钮一直是灰色无法点击? 1.1 问题描述 软件是从2W2E上下载的SWAT-CUP2019&#xff…

XILINX ZYNQ 7000 BOOT

参考UG585 内容 下面这张图是ZYNQ启动的关键流程 1.POR表示硬件复位&#xff0c;不关心Power-up也就是说冷热启动都行。Nor-POR就是非POR复位&#xff0c;有点软件应用复位的意思。 2.POR复位会复位所有寄存器。并且采集 HardWare boot pin的状态。这点很关键&#xff0c; 3.是…

mysql一些常用函数

group_concat()函数首先根据group by指定的列进行分组&#xff0c;并且用分隔符分隔&#xff0c;将同一个分组中的值连接起来&#xff0c;返回一个字符串结果。 group_concat([distinct] 字段名 [order by 排序字段 asc/desc] [separator 分隔符])-- 指定排序方式和分隔符 se…

STM32F407 滴答定时器

介绍STM32F407滴答定时器配置方法、使用方式&#xff0c;封装延时函数得到精确的时间。 【1】介绍滴答定时器的章节 STM32F407参考手册中第10章介绍了滴答定时器的校准值。 M4权威指南介绍滴答定时器的章节&#xff0c;M3权威指南中与M4权威指南中的介绍一样。 【2】滴答定时…

Windows 引导启动流程详述(BIOS-UEFI)

Windows 启动流程详述 BIOS 和 UEFI 的由来BIOS 存在哪里BIOS 程序的功能BIOS 和 UEFI 的发展由来如何查看当前计算机是什么方式引导启动呢&#xff1f;Linux 下如何查看 BIOS 大小&#xff1f; 启动流程详述使用 BIOS 进行系统启动流程使用 UEFI 进行系统启动流程SEC阶段PEI阶…

专项练习15

目录 一、选择题 1、如果要打开名为 “window2"的新窗口&#xff0c;可以通过&#xff08;&#xff09; 2、下列事件哪个不是由鼠标触发的事件&#xff08;&#xff09; 3、Angular指令中哪种作用域可以继承父scope 4、下列哪些事件不支持冒泡?&#xff08;&#xff09;…

微信小程序学习记录2 案例分享<智能家居UI>

效果 思路 页面分为4块 前三块 采用同样的class 替换三张矢量图 绑定三个单片机返回的JSON值 最后一块又分为左右两部分 左边部分 采用switch组件 绑定三个事件 右边部分则是普通的文字

记录生产mysql死锁解决过程

最近生产上每个星期都会有几次死锁告警异常&#xff0c;今天终于给处理了&#xff0c;待后续观察&#xff0c;记录下整个过程。 环境&#xff1a;springboot、mybatis、mysql(RC隔离级别) 表结构&#xff1a; CREATE TABLE table1 (id bigint NOT NULL AUTO_INCREMENT,prize…

ASP.NET Core MVC 从入门到精通系列文章(完)

随着技术的发展&#xff0c;ASP.NET Core MVC也推出了好长时间&#xff0c;经过不断的版本更新迭代&#xff0c;已经越来越完善&#xff0c;本系列文章主要讲解ASP.NET Core MVC开发B/S系统过程中所涉及到的相关内容&#xff0c;适用于初学者&#xff0c;在校毕业生&#xff0c…