当你输入 () 的时候,一定要确保输入完整,它们是成对出现的。对于 input 来说,你还可以给用户放一个提示,让他知道该输入什么。你可以把提示的字符串放在 () 里面,就像这样:
y = input(“Name?”)
这个提示告诉用户输入“名字”,然后把结果放到变量 y 里面。通过这种方式你就可以问用户问题然后得
到他输入的答案。
ex12.py
代码
age = int(input("How old are you? "))
height = input(f"You're {age}? Nice. How tall are you? ")
weight = input("How much do you weigh? ")
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
'''-----------------------------------------------------------------------------'''
'''
1. 在 Terminal 里输入 pydoc input ,看看它会说什么。如果你用的是 Windows, 输入
python3.6 -m pydoc input 。
Help on built-in function input in module builtins:
input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
2. 输入 q ,退出 pydoc 。
3. 到网上查查 pydoc 命令的作用。
pydoc是python自带的一个文档生成工具,使用pydoc可以很方便的查看类和方法结构
4. 用 pydoc 读一读关于 open , file , os ,和 sys 的内容;浏览一遍即可,把有意思的东
西记下来。
open:Help on built-in function open in module io:
open() returns a file object whose type depends on the mode, and
through which the standard file operations such as reading and writing
are performed.
os:Help on module os:
os - OS routines for NT or Posix depending on what system we're on.
sys:
https://docs.python.org/3.7/library/sys
该模块提供了一些变量和函数。这些变量可能被解释器使用,也可能由解释器提供。这些函数会影
响解释器。本模块总是可用的。
'''
运行结果
问题
为什么我每次运行 pydoc 都会收到错误信息: SyntaxError: invalid syntax ?
要么你没在命令行运行pydoc ,要么你先运行了 python3.6,先退出 python3.6 再运行 pydoc 。
为什么我的 pydoc 没有像你的一样暂停?
有时候如果帮助文件很短,一屏足以放下的话, pydoc 就只会把它打印出来。
当我运行 pydoc 的时候我会收到 more is not recognized as an internal 。
一些 Windows 版本没有这个命令,你可以跳过这个小题,需要它的时候在网上搜搜 Python documentation 即可。
为什么我不能用 print(“How old are you?”, input()) ?
你能,只不过 input() 的结果不会被保存到一个变量里,它会以一种奇怪的方式运行。你可以试试,然后试着打印你输入的东西,看看你能不能搞明白为什么它无法运行。