以下是一个测试测试用户正确感知时间流逝能力的新程序:
- #!/usr/bin/env python3
- from mymodularity import timestamp
- print("Press the RETURN key. Count to 3, and press RETURN again.")
- input()
- timestamp.Timer("Started timer at ")
- print("Count to 3...")
- input()
- timestamp.Timer("You slept until ")
将你的新程序保存为 response.py,运行它:
- $ python3 ./response.py
- Press the RETURN key. Count to 3, and press RETURN again.
- Started timer at 1560714482.3772075
- Count to 3...
- You slept until 1560714484.1628013
函数和所需参数
新版本的 timestamp 模块现在 需要 一个 msg 参数。这很重要,因为你的第一个应用程序将无法运行,因为它没有将字符串传递给 timestamp.Timer 函数:
- $ python3 ./sleeptest.py
- Testing Python sleep()...
- Traceback (most recent call last):
- File "./sleeptest.py", line 8, in <module>
- timestamp.Timer()
- TypeError: Timer() missing 1 required positional argument: 'msg'
你能修复你的 sleeptest.py 应用程序,以便它能够与更新后的模块一起正确运行吗?
变量和函数
通过设计,函数限制了变量的范围。换句话说,如果在函数内创建一个变量,那么这个变量 只 在这个函数内起作用。如果你尝试在函数外部使用函数内部出现的变量,就会发生错误。
下面是对 response.py 应用程序的修改,尝试从 timestamp.Timer() 函数外部打印 msg 变量:
- #!/usr/bin/env python3
- from mymodularity import timestamp
- print("Press the RETURN key. Count to 3, and press RETURN again.")
- input()
- timestamp.Timer("Started timer at ")
- print("Count to 3...")
- input()
- timestamp.Timer("You slept for ")
- print(msg)
试着运行它,查看错误:
- $ python3 ./response.py
- Press the RETURN key. Count to 3, and press RETURN again.
- Started timer at 1560719527.7862902
- Count to 3...
- You slept for 1560719528.135406
- Traceback (most recent call last):
- File "./response.py", line 15, in <module>
- print(msg)
- NameError: name 'msg' is not defined
(编辑:西安站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|