1

This is the code:

class Deck:
    suits = ['Spades', 'Clubs', 'Diamonds', 'Hearts']
    nums = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
    deck = [f"{j} of {i}" for j in nums for i in suits]
    #print(deck)

But when it compiled, it gave me this error:

deck = [f"{j} of {i}" for j in nums for i in suits]

NameError: name 'suits' is not defined

I have literally no idea why--although I am pretty new to OOP and trying to test it out on some of my projects.

  • Not exactly, but thanks – anonymouse69 Feb 18 '21 at 17:55
  • The weird thing is that--the list 'nums' prints out fine if i remove 'suits', but not if i keep them both – anonymouse69 Feb 18 '21 at 17:56
  • What is your expected output in `[f"{j} of {i}" for j in nums for i in suits]`? – Countour-Integral Feb 18 '21 at 17:59
  • I cannot reproduce the error on py3.6 - https://trinket.io/python3/136656d45b – ChatoPaka Feb 18 '21 at 18:00
  • @ChatoPaka the code you listed there is not inside a class (here it is inside `class Deck`) – Countour-Integral Feb 18 '21 at 18:02
  • @Countour-Integral: ['2 of Spades', '2 of Clubs', '2 of Diamonds', '2 of Hearts', '3 of Spades', '3 of Clubs', '3 of Diamonds', '3 of Hearts', '4 of Spades'.... and so on] – anonymouse69 Feb 18 '21 at 18:06
  • The bare variable declarations inside the `class` do not look inside the class unless you ask them to. They can't until the class is actually defined. Basically duplicate of https://stackoverflow.com/questions/13037426/how-to-access-class-scope-variables-without-self – tripleee Feb 18 '21 at 18:06
  • It works totally fine if i'm running the code outside of the class, but errors come in when I'm running it inside of the class. – anonymouse69 Feb 18 '21 at 18:06

0 Answers0