第五十一课:模块:__name__='__main__'、搜索路

2018-08-27  本文已影响0人  无罪的坏人

内容来源于网络,本人只是在此稍作整理,如有涉及版权问题,归小甲鱼官方所有。

练习题(来自小甲鱼官方论坛)

0.请写下这一节课你学习到的内容:格式不限,回忆并复述是加强记忆的好方式!

# p13_7.py
def c2f(cel):
    fah = cel * 1.8 +32
    return fah

def f2c(fah):
    cel = (fah - 32) / 1.8
    return cel

def test():
    print("测试,0摄氏度 = %.2f 华氏度" % c2f(0))
    print("测试,0华氏度 = %.2f 摄氏度" % f2c(0))

if __name__ == '__main__':
    test()

上面代码确保只有单独运行p13_7.py时才会执行test()函数。

>>> import sys
>>> sys.path
['', '/Users/liujin/Documents', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/easygui-0.98.0_UNRELEASED-py3.7.egg']
>>> sys.path.append('/Users/liujin/Documents/virtualenv3.7Demo/ven2')
>>> sys.path
['', '/Users/liujin/Documents', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/easygui-0.98.0_UNRELEASED-py3.7.egg', '/Users/liujin/Documents/virtualenv3.7Demo/ven2']
>>> import p13_7 as tc
>>> print("32摄氏度 = %.2f 华氏度" % tc.c2f(32))
32摄氏度 = 89.60 华氏度
>>> 

上一篇下一篇

猜你喜欢

热点阅读