Quiz 3: Conditional Execution | 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 3: Conditional Execution

We look at how Python executes some statements and skips others.



单选题(1-10)

  1. What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?
  • Begin the statement with a curly brace {
  • Indent the line below the if statement
  • Start the statement with a “#” character
  • Underline all of the conditional code
  1. Which of these operators is not a comparison / logical operator?
  • =
  • >
  • !=
  • ==
  • >=
  1. What is true about the following code segment:
if  x == 5 :
    print('Is 5')
    print('Is Still 5')
    print('Third 5')
  • The string ‘Is 5’ will always print out regardless of the value for x.
  • Only two of the three print statements will print out if the value of x is less than zero.
  • The string ‘Is 5’ will never print out regardless of the value for x.
  • Depending on the value of x, either all three of the print statements will execute or none of the statements will execute
  1. When you have multiple lines in an if block, how do you indicate the end of the if block?
  • You omit the semicolon ; on the last line of the if block
  • You capitalize the first letter of the line following the end of the if block
  • You use a curly brace { after the last line of the if block
  • You de-indent the next line past the if block to the same level of indent as the original if statement
  1. You look at the following text:
if x == 6 :
    print('Is 6')
    print('Is Still 6')
    print('Third 6')

It looks perfect but Python is giving you an ‘Indentation Error’ on the second print statement. What is the most likely reason?

  • In order to make humans feel inadequate, Python randomly emits ‘Indentation Errors’ on perfectly good code - after about an hour the error will just go away without any changes to your program
  • You have mixed tabs and spaces in the file
  • Python has reached its limit on the largest Python program that can be run
  • Python thinks ‘Still’ is a mis-spelled word in the string
  1. What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?
  • A closing curly brace followed by an open curly brace like this }{
  • otherwise
  • iterate
  • else
  1. What will the following code print out?
x = 0
if x < 2 :
    print('Small')
elif x < 10 :
    print('Medium')
else :
    print('LARGE')
print('All done')
  • Medium All done
  • Small
  • Small Medium LARGE All done
  • Small All done
  1. For the following code,
if x < 2 :
    print('Below 2')
elif x >= 2 :
     print('Two or more')
else :
    print('Something else')

What value of ‘x’ will cause ‘Something else’ to print out?

  • This code will never print ‘Something else’ regardless of the value for ‘x’
  • x = 2
  • x = 2.0
  • x = -2
  1. In the following code (numbers added) - which will be the last line to execute successfully?
(1)   astr = 'Hello Bob'
(2)   istr = int(astr)
(3)   print('First', istr)
(4)   astr = '123'
(5)   istr = int(astr)
(6)   print('Second', istr)
  • 2
  • 3
  • 6
  • 1
  1. For the following code:
astr = 'Hello Bob'
istr = 0
try:
    istr = int(astr)
except:
    istr = -1

What will the value be for istr after this code executes?

  • The istr variable will not have a value
  • 0
  • -1
  • It will be the ‘Not a number’ value (i.e. NaN)

编程题

Exercise 3.1

题目:
Write a program to prompt the user for hours and rate per hour using input to compute gross pay.
Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours.
Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75).
You should use input to read a string and float() to convert the string to a number.
Do not worry about error checking the user input - assume the user types numbers properly.

[Desired Output]
498.75

解题代码:

hrs = input("Enter Hours:")
rate = input("Enter Hourly rate:")

h = float(hrs)
rate = float(rate)

if h <= 40:
   per = rate * h
else:
   per = rate * 40 + rate * 1.5 * (h - 40)

print(per)

Exercise 3.3

题目:
Write a program to prompt for a score between 0.0 and 1.0.
If the score is out of range, print an error.
If the score is between 0.0 and 1.0, print a grade using the following table:
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
If the user enters a value out of range, print a suitable error message and exit.
For the test, enter a score of 0.85.

[Desired Output]
B

解题代码:

score = input("Enter Score: ")
score = float(score)

if 0.0 < score < 1.0:
   if score >= 0.9:
       print('A')
   elif score >= 0.8:
       print('B')
   elif score >= 0.7:
       print('C')
   elif score >= 0.6:
       print('D')
   else:
       print('F')
else:
   print("error")
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

冰.封万里

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

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

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

打赏作者

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

抵扣说明:

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

余额充值