|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请教鱼油高手:
我编辑了一个模块,模块名temperature,代码如下:
def fahrenheit_to_celsius(fahrenheit:float)->float:
"""the number of fahrenheit temperature\
convert to the celsius one"""
celsius_tem = (fahrenheit-32)*5/9
return celsius_tem
def above_freezing(celsius:float)->bool:
"""return True when the celsius temperature\
above zero"""
return celsius > 0
if __name__ == '__main__':
fahrenheit = float(input('Pls key in the Fahrenheit temperature:'))
celsius = fahrenheit_to_celsius(fahrenheit)
if above_freezing(celsius):
print('The temperature is above freezing')
else:
print('The temperature is below freezing')
然后我用doctest去测试,但是不运行(attempted=0):
Pls key in the Fahrenheit temperature:500
The temperature is above freezing
>>> import doctest
>>> doctest.testmod()
TestResults(failed=0, attempted=0)
>>>
模块里有三个函数,照道理应该调用三次,就是attempted = 3, 但是为什么会这样?
本帖最后由 suchocolate 于 2021-9-1 11:17 编辑
两文件放到一个文件夹下:
temperature.py
- def fahrenheit_to_celsius(fahrenheit: float) -> float:
- celsius_tem = (fahrenheit - 32) * 5 / 9
- return celsius_tem
- def above_freezing(celsius: float) -> bool:
- return celsius > 0
复制代码
test.txt
- >>> from temperature import *
- >>> fahrenheit_to_celsius(80)
- 26.666666666666668
- >>> above_freezing(30)
- True
复制代码
cmd跳到相应路径,执行:
- python -m doctest -v test.txt
复制代码
|
|