DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+2 votes

I'm getting AttributeError: 'NoneType' object has no attribute 'group'

mymodule_test.py

import unittest

from unittest import TestCase

from mymodule import get_percentage


class GetCardsTestCase(TestCase):
    def test_get_percentage(self):
        s = "92 (1%)"
        expected = 1
        actual = get_percentage(s)
        self.assertEqual(actual, expected)


if __name__ == '__main__':
    unittest.main()

mymodule.py

import re


def get_percentage(s: str) -> int:
    return int(re.match(r'\(([0-9]*)%\)', s).group(1))
by
0

What are you actually trying to achieve here? Do you have an input string with an integer between ( and %), and you want to return the value of it?

1 Answer

+1 vote
 
Best answer

re.match only matches at the beginning of the string. You have to use re.search instead.

by
selected by
Contributions licensed under CC0
...