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


课程简介

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰.封万里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值