跟着笨方法学python个教程学习,边学边做习题边记录。
新手学Python-笨方法学python-ex12-提示别人 - 学习记录
课程内容:
当你键入 raw_input() 的时候,你需要键入 ( 和 ) 也就是“括号(parenthesis)”。这和你格式化输出两个以上变量时的情况有点类似,比如说 "%s %s" % (x, y) 里边就有括号。对于 raw_input 而言,你还可以让它显示出一个提示,从而告诉别人应该输入什么东西。你可以在 () 之间放入一个你想要作为提示的字符串,如下所示:
y = raw_input("Name? ")
这句话会用 “Name?” 提示用户,然后将用户输入的结果赋值给变量 y。这就是我们提问用户并且得到答案的方式。
也就是说,我们的上一个练习可以使用 raw_input 重写一次。所有的提示都可以通过 raw_input 实现。
跟着课程练习:
[root@lc test]# vi ex12.py
age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")
print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)
"ex12.py" 5L, 198C written
[root@lc test]#
执行该脚本:
[root@lc test]# python ex12.py
How old are you? 35
How tall are you? 6'2"
How much do you weigh? 180lbs
So, you're '35' old, '6\'2"' tall and '180lbs' heavy.
[root@lc test]#
加分习题
1.在命令行界面下运行你的程序,然后在命令行输入 pydoc raw_input 看它说了些什么。如果你用的是 Window,那就试一下 python -m pydoc raw_input 。
[root@lc test]# python -m pydoc raw_input
Help on built-in function raw_input in module __builtin__:
raw_input(...)
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.
(END)
2.输入 q 退出 pydoc。
[root@lc test]# python -m pydoc raw_input
Help on built-in function raw_input in module __builtin__:
raw_input(...)
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.
[root@lc test]#
3.上网找一下 pydoc 命令是用来做什么的。
[root@lc test]# python -m pydoc
pydoc - the Python documentation tool
pydoc.py <name> ...
Show text documentation on something. <name> may be the name of a
Python keyword, topic, function, module, or package, or a dotted
reference to a class or function within a module or module in a
package. If <name> contains a '/', it is used as the path to a
Python source file to document. If name is 'keywords', 'topics',
or 'modules', a listing of these things is displayed.
pydoc.py -k <keyword>
Search for a keyword in the synopsis lines of all available modules.
pydoc.py -p <port>
Start an HTTP server on the given port on the local machine.
pydoc.py -g
Pop up a graphical interface for finding and serving documentation.
pydoc.py -w <name> ...
Write out the HTML documentation for a module to a file in the current
directory. If <name> contains a '/', it is treated as a filename; if
it names a directory, documentation is written for all the contents.
[root@lc test]# python -m pydoc -p 8888
然后使用http://localhost:8888可以看到所有模块说明的文档
4.使用 pydoc 再看一下 open, file, os, 和 sys 的含义。看不懂没关系,只要通读一下,记下你觉得有意思的点就行了。
python -m pydoc open
python -m pydoc file
python -m pydoc os
python -m pydoc sys
至此,笨方法学Python-ex12-提示别人这一小节的学习结束了。