This notebook is an exercise in the Python course. You can reference the tutorial at this link.
Try It Yourself
Functions are powerful. Try writing some yourself.
As before, don't forget to run the setup code below before jumping into question 1.
自己尝试一下
函数功能强大。 尝试自己写一些。
和以前一样,在进入问题 1 之前,不要忘记运行下面的设置代码。
# SETUP. You don't need to worry for now about what this code does or how it works.
from learntools.core import binder; binder.bind(globals())
from learntools.python.ex2 import *
print('Setup complete.')
Setup complete.
Exercises
练习
1.
Complete the body of the following function according to its docstring.
HINT: Python has a built-in function round
.
1.
根据文档字符串完成以下函数的主体。
提示:Python 有一个内置函数round
。
def round_to_two_places(num):
"""Return the given number rounded to two decimal places.
>>> round_to_two_places(3.14159)
3.14
"""
# Replace this body with your own code.
# ("pass" is a keyword that does literally nothing. We used it as a placeholder
# because after we begin a code block, Python requires at least one line of code)
# pass
return round(num, 2)
# Check your answer
q1.check()
Correct
# Uncomment the following for a hint
#q1.hint()
# Or uncomment the following to peek at the solution
#q1.solution()
2.
The help for round
says that ndigits
(the second argument) may be negative.
What do you think will happen when it is? Try some examples in the following cell?
Can you think of a case where this would be useful?
2.
round
的帮助表明ndigits
(第二个参数)可能为负数。
你认为届时会发生什么? 尝试以下单元格中的一些示例?
你能想出一个这会有用的案例吗?
import math
# Put your test code here
round(12345.6789,-1)
12350.0
# Check your answer (Run this code cell to receive credit!)
q2.solution()
Solution: As you've seen, ndigits=-1
rounds to the nearest 10, ndigits=-2
rounds to the nearest 100 and so on. Where might this be useful? Suppose we're dealing with large numbers:
The area of Finland is 338,424 km²
The area of Greenland is 2,166,086 km²
We probably don't care whether it's really 338,424, or 338,425, or 338,177. All those digits of accuracy are just distracting. We can chop them off by calling round()
with ndigits=-3
:
The area of Finland is 338,000 km²
The area of Greenland is 2,166,000 km²
(We'll talk about how we would get the commas later when we talk about string formatting :))
3.
In a previous programming problem, the candy-sharing friends Alice, Bob and Carol tried to split candies evenly. For the sake of their friendship, any candies left over would be smashed. For example, if they collectively bring home 91 candies, they'll take 30 each and smash 1.
Below is a simple function that will calculate the number of candies to smash for any number of total candies.
Modify it so that it optionally takes a second argument representing the number of friends the candies are being split between. If no second argument is provided, it should assume 3 friends, as before.
Update the docstring to reflect this new behaviour.
3.
在之前的编程问题中,分享糖果的朋友爱丽丝、鲍勃和卡罗尔试图平均分配糖果。 为了友谊,剩下的糖果都会被砸碎。 例如,如果他们总共带回家 91 颗糖果,他们将每人拿 30 颗并粉碎 1 颗。
下面是一个简单的函数,它将计算任意总糖果数量需要粉碎的糖果数量。
修改它,以便它可以选择使用第二个参数来表示糖果被分配的朋友数量。 如果没有提供第二个参数,它应该像以前一样假设有 3 个朋友。
更新文档字符串以反映这一新行为。
def to_smash(total_candies, number_of_friends=3):
"""Return the number of leftover candies that must be smashed after distributing
the given number of candies evenly between 3 friends.
>>> to_smash(91)
1
"""
return total_candies % number_of_friends
# Check your answer
q3.check()
Correct
#q3.hint()
#q3.solution()
4. (Optional)
It may not be fun, but reading and understanding error messages will be an important part of your Python career.
Each code cell below contains some commented-out buggy code. For each cell...
- Read the code and predict what you think will happen when it's run.
- Then uncomment the code and run it to see what happens. (Tip: In the kernel editor, you can highlight several lines and press
ctrl
+/
to toggle commenting.) - Fix the code (so that it accomplishes its intended purpose without throwing an exception)
4.(可选)
这可能并不有趣,但阅读和理解错误消息将是你的 Python 职业生涯的重要组成部分。
下面的每个代码单元格都包含一些注释掉的错误代码。 对于每个细胞...
- 阅读代码并预测运行时会发生什么。
- 然后取消代码注释并运行它看看会发生什么。 (提示:在内核编辑器中,您可以突出显示几行并按
ctrl
+/
来切换注释。) - 修复代码(使其达到预期目的而不抛出异常)
# ruound_to_two_places(9.9999)
# x = -10
# y = 5
# # Which of the two variables above has the smallest absolute value?
# smallest_abs = min(abs(x, y))
# def f(x):
# y = abs(x)
# return y
# print(f(5))
Keep Going
Nice job with the code. Next up, you'll learn about conditionals, which you'll need to write interesting programs. Keep going here
继续
代码做得很好。 接下来,您将学习条件,您将需要它来编写有趣的程序。 继续此处