还是书上的练习题,第一次创作模块还蛮有意思的。纪念一下(~ ̄▽ ̄)~
首先建立模块
#this is the file "my_module.py"
#we're going to use it in another program
def c_to_f(celsius):
fahrenheit=celsius*9.0/5+32
return fahrenheit
然后在需要的时候调用这个模块
import my_module
celsius=float(input('Enter a temperature in Celsius:'))
fahrenheit=my_module.c_to_f(celsius)
print("That's",fahrenheit,"degrees Fahrenheit")
我们就可以在需要的时候导入这个模块,计算摄氏度转华氏度啦ヾ(๑╹◡╹)ノ"
本文介绍了一个简单的模块化编程实例,通过创建和使用自定义模块来实现摄氏度到华氏度的转换。该模块可以在多个程序中重复使用,展示了Python模块化的便捷性和实用性。
2059

被折叠的 条评论
为什么被折叠?



