Discuss / Python / 小白求助,求各位大神帮我看看,非常感谢

小白求助,求各位大神帮我看看,非常感谢

Topic source

import unittest

class Student(object):

    def __init__(self,name,score):

        self.name=name

        self.score=score

    def get_score(self):

        if self.score<0 or self.score>100:

            raise ValueError('score value is wrong')

        if self.score>=80:

            return 'A'

        if 80>self.score>=60:

            return 'B'

        return 'C'

#测试单元

class TestStudent(unittest.TestCase):

    def test_80_to_100(self):

        s1=Student('Bart',80)

        s2=Student('Lisa',100)

        self.assertEqual(s1.get_grade(),'A')

        self.assertEqual(s2.get_grade(),'A')

    def test_60_to_80(self):

        s1=Student('Bart',60)

        s2=Student('Lisa',79)

        self.assertEqual(s1.get_grade(),'B')

        self.assertEqual(s2.get_grade(),'B')

    def test_0_to_60(self):

        s1=Student('Bart',0)

        s2=Student('Lisa',59)

        self.assertEqual(s1.get_grade(),'C')

        self.assertEqual(s2.get_grade(),'C')

    def test_invalid(self):

        s1=Student('Bart',-1)

        s2=Student('Lisa',101)

        with self.assertRaises(ValueError):

            s1.get_grade()

        with self.assertRaises(ValueError):

            s2.get_grade()

if __name__=='__main__':  #在最后加上这两行代码,就可以运行单元测试

    unittest.main()

在Visual Studio Code调试出现问题,显示如下:

Traceback (most recent call last):

  File "aha.py", line 16, in <module>

    import unittest

  File "C:\Users\13718\AppData\Local\Programs\Python\Python38\lib\unittest\__init__.py", line 59, in <module>

    from .result import TestResult

  File "C:\Users\13718\AppData\Local\Programs\Python\Python38\lib\unittest\result.py", line 5, in <module>

    import traceback

  File "C:\Users\13718\AppData\Local\Programs\Python\Python38\lib\traceback.py", line 5, in <module>

    import linecache

  File "C:\Users\13718\AppData\Local\Programs\Python\Python38\lib\linecache.py", line 11, in <module>

    import tokenize

  File "C:\Users\13718\AppData\Local\Programs\Python\Python38\lib\tokenize.py", line 32, in <module>

    import re

  File "C:\Users\13718\AppData\Local\Programs\Python\Python38\lib\re.py", line 122, in <module>

    import enum

  File "C:\pythonexample\enum.py", line 8

    def__init__(self,name,gender):

                                 ^

SyntaxError: invalid syntax

哪位大神能告知是什么问题?

你的方法get_score和测试用例的方法 get_grade不一致,看仔细点

bug管理员

#3 Created at ... [Delete] [Delete and Lock User]

你这个怎么显示成代码格式的,我代码复制进来,发布就缩略成了 文本格式了


  • 1

Reply