Python-習題11~15

習題  11:提問python

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)

運行結果:linux

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.

加分題:shell

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:提示別人spa

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)

運行結果:

相關文章
相關標籤/搜索