码迷,mamicode.com
首页 > 编程语言 > 详细

python 第四天

时间:2017-09-16 11:56:53      阅读:171      评论:0      收藏:0      [点我收藏+]

标签: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

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

fahrenheit =c_to_f(celsius) #这里就不用指明哪个模块的哪个函数。直接使用函数就行

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





本文出自 “姑苏城” 博客,请务必保留此出处http://ji123.blog.51cto.com/11333309/1965758

python 第四天

标签:python

原文地址:http://ji123.blog.51cto.com/11333309/1965758

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!