笨办法学Python记录--习题12-14 主要是pydoc用法,raw_input,argv
20140413 -- 习题12 - 14
1. pydoc在windows的用法,必须进入到python安装目录,执行Python -m pydoc raw_input;
网上给出了一个好玩的,不过只能查到文档级别:在命令行到pydoc所在的目录python的安装目录lib下,运行 python pydoc.py -p 8877 ,其中8877是随便能用的端口即可。之后在IE中打开“http://127.0.0.1:8877/ 你会有意外惊喜。”
查到的信息如下:
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.
prompt译作提示,读取一个字符串,从标准输入中。紧接着是等待输入。如果用户使用了结束键,就抛出EOFError。在Unix,GNU被用到了,提示字符串事先被打印出来。
PS C:\Python27> python -m pydoc open
Help on built-in function open in module __builtin__:
open(...)
open(name[, mode[, buffering]]) -> file object
Open a file using the file() type, returns a file object. This is the
preferred way to open a file. See file.__doc__ for further information.
返回的是file对象。
2. argv ---相当于shell中的$参数,用法如下:
from sys import argv
参数1,参数2 = argv #之后参数的饮用就和这个先后顺序有关了
3. 配合raw_input使用argv效果不错,例如
from sys import argv
script,user_name = argv
prompt = '>'
print "My name is %s, and you know I am the %s script." %(user_name,script)
print "do u like me?"
like = raw_input(prompt)
print "how old r u?"
age = raw_input(prompt)
print """
OK, you said you %r me very much. you r %r years old.
""" %(like,age)