2

I'm creating a simple game using an standard deck of cards. At the top, I'm declaring the following:

suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
         'Eight', 'Nine','Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7,
          'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,
          'Queen':10, 'King':10, 'Ace':11}

Down below, I use a playing variable to control the game as follows in simplified version:

while True:
#Initiate the game, instructions etc.
playing = True
if this_happens:
    playing = False
break

However, when I run my code through pylint, it returns Constant name "playing" doesn't conform to UPPER_CASE naming style (invalid-name).

I checked PEP8 for answer, but I didn't find what exactly is the case. I also tried using playing in a class, since, the top ones are, but the result was the same.

As I understand, variables declared outside of a class or function are global. Since this is the case, why does the following is show for playing alone since the others are also global? And what would be a good practice in this case?

Lucas
  • 91
  • 9
  • relevant https://stackoverflow.com/questions/54151197/why-does-pylint-require-capitalized-variable-names-when-outside-a-function – Chris_Rands Nov 18 '21 at 19:17
  • This is the only thing PyLint complains about?!? – Scott Hunter Nov 18 '21 at 19:18
  • 1
    I'm actually surprised you are you are making a simple card came and concerned about PEP8 and linting. This seems like an exercise rather than production code – Chris_Rands Nov 18 '21 at 19:21
  • 1
    @Chris_Rands Thank you for your comments! It is indeed an exercise. I'm still learning Python, but I wanted to improve my code and learn the best practices along the language itself, it seems easier for me. – Lucas Nov 18 '21 at 19:34
  • 1
    @ScottHunter Yes. – Lucas Nov 18 '21 at 19:34
  • B/C the code as posted generates a *lot* of complaints from PyLint. – Scott Hunter Nov 18 '21 at 19:43
  • @ScottHunter As I pointed at my question, the code is a simplified version. And yes, as I said before, this is the only error returned. – Lucas Nov 18 '21 at 19:55
  • 1
    Plyint assumes variables defined in the global scope are meant to be constant. – juanpa.arrivillaga Nov 18 '21 at 20:18
  • @juanpa.arrivillaga Thank you for your answer. But what really puzzled me is that the other ones do not receive this error. After a few tests, it seems to be dependent on the data type of the variable. – Lucas Nov 18 '21 at 20:39
  • This question is not a duplicate as the other answer does not say why some global are considered constant or not by pylint. It's because pylint can infer that ``True`` is a constant while dict could be global value. See https://github.com/PyCQA/pylint/issues/4606 and https://github.com/PyCQA/pylint/commit/3422e4adc718c60a467d66ab0dedf4a7032c4fc5 for more detail – Pierre.Sassoulas Nov 20 '21 at 08:49

0 Answers0