Python-习题11~15

习题  11:提问

print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weight?",
weight = raw_input()

print "So,you're %r old,%rtall and %r heavy." %(
    age,height,weight)

运行结果:

How old are you? 35
How tall are you? 6'2''
how much do you weight? 180lb
So,you're '35' old,"6'2''"tall and '180lb' heavy.

加分题:

1.  raw_input 不管用户输入什么类型的都会转变成字符型

2.  另:input会根据用户输入变换相应的类型,而且如果要输入字符和字符串的时候必须要用引号包起来。

3.  自己再写一段类似格式的:

print "How many times did you go to the park?",
times = raw_input()
print "How often do you go home a week?",
fluence = raw_input()
print "What food is you favorite?",
food = raw_input()

print """So, you went the park %r times,go home %r times a week,
like %r very much."""%(times,fluence,food)

运行结果:

How many times did you go to the park? 4
How often do you go home a week? 3
What food is you favorite? fish
So, you went the park '4' times,go home '3' times a week,
like 'fish' very much.

 

 

 

 

习题  12:提示别人

y = raw_input("Name?")   # Name? 起到了提示别人的作用

再练习一次习题11的内容:

age = raw_input("How old are you? ")
height = raw_input("How tall are you?")
weight = raw_input("How much do you weight?")

print "So you're %r old, %r tall and %r heavy."%(
    age,height,weight)

运行结果:

How old are you? 35
How tall are you?6'2''
How much do you weight?180lb
So you're '35' old, "6'2''" tall and '180lb' heavy.

加分题:

1.  在Linux中的命令行输入pydoc raw_input   的结果是

2.  pydoc 命令是文档生成工具:

python -m pydoc raw_input 

S C:\Documents and Settings\jdu> 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.

PS C:\Documents and Settings\jdu>


让我们来看一个使用大部分元素的简化示例:

清单 1: 附带典型文档的模块 mymod.py
 

#!/usr/bin/python
"""Show off features of [pydoc] module
This is a silly module to
demonstrate docstrings
"""
__author__ = 'David Mertz'
__version__= '1.0'
__nonsense__ = 'jabberwocky'
class MyClass:
    """Demonstrate class docstrings"""
    def __init__ (self, spam=1, eggs=2):
        """Set default attribute values only
        Keyword arguments:
        spam ― a processed meat product
        eggs ― a fine breakfast for lumberjacks
        """
        self.spam = spam
        self.eggs = eggs




pydoc 模块利用了 Python 文档的约定,又使用了一些有关 Python 导入、继承和其它类似的实用知识。此外, pydoc 有绝对的天赋可以使自己在不同的操作模式下被使用(马上就能看到更多有关这个论点的资料)。让我们用一些时间,看看通过 OS 命令行调用的 manpage 风格的用法。

假设您已将上述模块 mymod 安装在您的系统上,但不知道它有什么用处(在示例中并不多)。您可以阅读源代码,不过更简单的方法可能是:

清单 2:获取‘manpage’风格的文档
 

% pydoc.py mymod
Python Library Documentation: module mymod
NAME
    mymod - Show off features of [pydoc] module
FILE
    /articles/scratch/cp18/mymod.py
DESCRIPTION
    This is a silly module to
    demonstrate docstrings
CLASSES
    MyClass
    class MyClass
     | Demonstrate class docstrings
     |
     | __init__(self, spam=1, eggs=2)
     | Set default attribute values only
     |
     | Keyword arguments:
     | spam ― a processed meat product
     | eggs ― a fine breakfast for lumberjacks
DATA
    __author__ = 'David Mertz'
    __file__ = './mymod.pyc'
    __name__ = 'mymod'
    __nonsense__ = 'jabberwocky'
    __version__ = '1.0'
VERSION
    1.0
AUTHOR
    David Mertz




根据特定的平台和安装过程,上述样本可能会显示在一个允许滚屏、搜索等功能并突出显示某些关键字的文本查看器中。对于像这样简单的示例,只是比纯粹的阅读源代码好一点。但请考虑一下像下面这样简单的示例:

清单 3:检查类的继承结构
 

% cat mymod2.py
from mymod import MyClass
class MyClass2(MyClass):
    """Child class"""
    def foo(self):
        pass
% pydoc.py mymod2.MyClass2
Python Library Documentation: class MyClass2 in mymod2
class MyClass2(mymod.MyClass)
 | Child class
 |
 | __init__(self, spam=1, eggs=2) from mymod.MyClass
 |
 | foo(self)




在这个快速报告中,我们可以知道 MyClass2 有 __init__() 和 foo() 方法(以及相应的参数),哪个方法是类自身实现的以及其它哪些方法是继承而来(以及被继承的类所处的位置)。

另一个美妙的类似于 manpage 的功能是用来在模块中搜索关键字的 -k 选项。例如:

清单 4:为任务定位适当的模块
 

% pydoc.py -k uuencode
uu - Implementation of the UUencode and UUdecode functions.
% pydoc.py uu
Python Library Documentation: module uu
NAME
    uu - Implementation of the UUencode and UUdecode functions.
[...]




pydoc 除了它的命令行用法之外,还有其它四种“模式”可以显示被生成的同样的文档。

    Shell 模式:在 Python 交互式 shell 中,您可以导入 pydoc 的 help() 函数,这样就能够在不离开交互式会话的情况下获得任何对象的帮助。也可以只输入一个 help 进入交互式“help 解释器”。例如:

    清单 5:shell 模式下的交互式 help 解释器

   

 #------- Interactive shell with help enhancements ------#
    >>> from pydoc import help
    >>> import uu
    >>> help(uu.test)
    Help on function test in module uu:
    test()
      uuencode/uudecode main program
    >>> help
    Welcome to Python 2.0! This is the online help utility.
    [...introductory message about help shell...]
    help>




    Web 服务器模式:仅使用 -p 选项, pydoc 就会在 LOCALHOST 上作为一个简单的 Web 服务器自启动。您可以使用任何 Web 浏览器浏览所有已安装在现有操作系统上的模块。这个服务器的主页是一张模块列表,根据目录(并用浏览器支持的醒目色块)将它们分组。此外,您查看其文档的每个模块也广泛分布着它导入的函数、方法以及指向任何模块的链接。
    HTML 生成器模式: -w 选项对于 pydoc 可以归档的任何文档都能生成 HTML 文档页面。这些页面与您在 Web 服务器模式下可能会浏览到的页面本质上是一回事,但页面是静态的,可以进行存档、传输等等。
    TK 浏览器模式: -g 选项将创建一个和 xman 或 tkman 风格很相似的“图形帮助浏览器。”

http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-19/

------------------------------------------------------------------------------------------------------------------------------------------------------

来自网页:http://m.douban.com/note/316798580

4.  使用pydoc 再看一下open,file,os,和sys的含义,通读了解。

 

 

 

习题  13:参数、 解包、变量

from sys import argv

script,first,second,third=argv

print "The script is called:",script
print "Your first variable is:",first
print "Your second variable is:",second
print "Your third variable is:",third

运行结果:

$ python ex13.py first 2nd 3rd
The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd

加分题:

 

 

 

习题  14:提示和传递

from sys import argv
script,user_name = argv
prompt = '>'

print "Hi %s,I'm the %s script." %(user_name,script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
like = raw_input(prompt)

print "Where do you live %s?" % user_name
lives = raw_input(prompt)

print """
Alright,so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
"""%(likes,lives,computer)

运行结果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/8824/blog/787043

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值