天天看點

python 第四天

class Game_object:

    def __init__(self,name):

        self.name=name

    def pickUp(self):

       pass       ##這裡原本不允許函數為空,是以可以使用pass 先略過。

        #put code here

        #to the player's collectiong

class Coin (Game_object):

    def __init__(self,value):

        Game_object.__init__(self)

        self.value =value

    def spend(self,buyer,seller):

        pass

        #put code here

建立個模闆

#this is a module.files

def c_to_f(celsius):

    fahrenheit = celsius * 9.0 / 5 + 32

    return (fahrenheit) #注意一定加括号,否則在python 3.0以後版本會報錯 

'return' outside function

儲存為my_module.py 

import my_module   #調用這個子產品 (這裡my_module 就是個命名空間)

celsius =float(input("Enter a temperature in Celesius:"))

fahrenheit =my_module.c_to_f(celsius) 注意調用子產品的函數一定要子產品和函數都要寫上。

print ("Thant's",fahrenheit,"degrees Fahrenheit")

以上的也可以用

from my_module import c_to_f

fahrenheit =c_to_f(celsius) #這裡就不用指明哪個子產品的哪個函數。直接使用函數就行

<b>本文轉自 神迹難覓 51CTO部落格,原文連結:http://blog.51cto.com/ji123/1965758,如需轉載請自行聯系原作者</b>