Geeker_odd 发表于 2021-8-30 11:21:50

为什么不运行测试

请教鱼油高手:

我编辑了一个模块,模块名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, 但是为什么会这样?


wp231957 发表于 2021-8-30 14:15:36

看了好些遍都没看明白你想问啥
ps为啥全都是英文啊   看着很不好,难道你不是大陆的

Geeker_odd 发表于 2021-8-30 16:55:58

wp231957 发表于 2021-8-30 14:15
看了好些遍都没看明白你想问啥
ps为啥全都是英文啊   看着很不好,难道你不是大陆的

不好意思!主要是使用习惯了。这个模块包含三个函数,华氏温度转摄氏读数,摄氏温度转华氏温度,是否结冰。如果用dotest测试代码,应该attempted=3才对,可是这里为0。我怀疑是否模块程序有误。

suchocolate 发表于 2021-9-1 09:42:19

本帖最后由 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

Geeker_odd 发表于 2021-9-2 10:22:51

suchocolate 发表于 2021-9-1 09:42
两文件放到一个文件夹下:
temperature.py



谢谢!我再试试

Geeker_odd 发表于 2021-9-2 10:25:44

suchocolate 发表于 2021-9-1 09:42
两文件放到一个文件夹下:
temperature.py



不能在IDLE下运行吗?我不太会用DOS

Geeker_odd 发表于 2021-9-2 10:37:15

suchocolate 发表于 2021-9-1 09:42
两文件放到一个文件夹下:
temperature.py



还是不行啊........

suchocolate 发表于 2021-9-2 11:02:00

Geeker_odd 发表于 2021-9-2 10:37
还是不行啊........

cmd下不用进python
学程序cmd基础功能还是要会的。
页: [1]
查看完整版本: 为什么不运行测试