-1

I'm trying to make a game in python and am organizing my data.

game={
    'img':{
        'bg':'wouir'
    }
}
print(stuff.img.bg)

When I push build it has an error on the last line saying:
AttributeError: 'dict' object has no attribute 'img'

It seems I have a problem, what is it?

DucksEL
  • 57
  • 5
  • 12
    Python is not Javascript, dictionary keys are not attributes, and accessing with square brackets like `game['img']` is not interchangeable with dot notation like `game.img`. – kaya3 Jan 29 '22 at 02:28
  • 1
    "It seems I have a problem, what is it?" Well, in your own words, what does `attribute` mean in a Python program? *According to the sources you used to learn Python*, how is data retrieved from a dictionary? – Karl Knechtel Jan 29 '22 at 04:25
  • [Accessing elements of Python dictionary by index](https://stackoverflow.com/q/5404665/15497888) – Henry Ecker May 22 '22 at 21:43

2 Answers2

1

In Python, the keys of a dictionary are not its attributes. This means you cannot call them with the .. You have to use ['img'] instead of .img.

Change your loop to this:

while True:
    screen.draw(game['img']['bg'], (0,0))    # I've only corrected game.img.bg
CrazyChucky
  • 3,263
  • 4
  • 11
  • 25
Codeman
  • 477
  • 3
  • 13
1

You cannot access object's items through dot-notation in Python. You're supposed to use braces.

screen.draw(game["img"]["bg"], (0,0)) 
Syllight
  • 333
  • 1
  • 9