AttributeError: 'Settings' object has no attribute 'screen_width'? How to resolve this issue?

python attributeerror object has no attribute
attributeerror django
typeerror python
python import attributeerror
python custom exception
attributeerror pplayoutblank
attributeerror meaning
filenotfounderror python

The issue is that, when I build this code in sublime text; it gives me an output as AttributeError: 'Settings' object has no attribute 'screen_width'. which is an error. I don't know, how can I resolve this? Although I had searched for that in stack overflow, those answers don't solve this problem. Can anyone help me by going through this code and identifying how to fix this issue? Here is my alieninvasion.py code:

import sys
import pygame
from settings import Settings

def run_game():
    #Intiialize game and create a screen object
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    #start the main loop for the game
    while True:

        #redraw the screen during each pass though the loop
        screen.fill(ai_settings.bg_color)

        # Watch for Keyboard  and mouse events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                    sys.exit()

    # make th emost recently drawn screen visible
    pygame.display.flip()


run_game()

Here is my setting.py code

class Settings():
   """A class to store all the settings for Aliean Invasion """

   def __init__(self):
       """Initialize the game settings"""
       #screen settings
       self.screen_width = 1200
       self.screen_height = 800
       self.bg_color = (230, 230, 230)

Here is the return I am getting.

Traceback (most recent call last):
File "AlienInvasion.py", line 34, in <module>
run_game()
File "AlienInvasion.py", line 15, in run_game
(ai_settings.screen_width, ai_settings.screen_height))
AttributeError: 'Settings' object has no attribute 'screen_width'

I look forward to finding out what simple mistake I am unable to see.

Your Settings class doesn’t have screen_width, you should use ai.screen_width.

exception AttributeError ¶. Raised when an attribute reference (see Attribute references) or assignment fails. (When an object does not support  The AttributeError in Python is raised when an invalid attribute reference is made, or when an attribute assignment fails. While most objects support attributes, those that do not will merely raise a TypeError when an attribute access attempt is made.

This should be the correct code:

alieninvasion.py:

#!/usr/bin/env python
import sys
import pygame
from setting import Settings


def run_game():
    # Initialize game and create a screen object
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    # start the main loop for the game
    while True:

        # redraw the screen during each pass though the loop
        screen.fill(ai_settings.bg_color)

        # Watch for Keyboard  and mouse events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # make the most recently drawn screen visible
        pygame.display.flip()


run_game()

setting.py:

class Settings:
    """A class to store all the settings for Alien Invasion """

    def __init__(self):
        """Initialize the game settings"""
        # screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)
        self.ship_speed_factor = 8

In alieninvasion.py you have the wrong indent for pygame.display.flip(). Indent same amount of while True statement will cause this line unreachable. I changed it in my code for reference as well as some typos.

In most other programming languages, indentation is used only to help make the code look pretty. But in Python, it is required for indicating what block of code a statement belongs to.

The AttributeError in Python is raised when an invalid attribute reference is made, or when an attribute assignment fails. While most objects  I usually think about attributes as nouns that belong to an object. For example, “the student has two eyes ”. But in Python, an attribute can also be an action that an object can perform—“The cat can jump ”. An attribute in Python means some property that is associated with a particular type of object.

to solve the (AttributeError: 'Settings' object has no attribute 'screen_width') you must put in your class settings the following self.ai_screen_width = 1200 and in alieninvasion.py you place, screen = pygame.display.set_mode ((ai_settings. ai_screen_width, ai_settings.ai_screen_height))

Note: sorry for my English, I really don't speak it very well.

These errors yields to the program not executed. One of the error in Python mostly occurs is “ AttributeError “. AttributeError can be defined as an error that is raised  AttributeError: module 'tensorflow' has no attribute 'Summary' #9. Open palunel opened this issue Apr 23, 2019 · 1 comment Open

Attribute Error. Attributes in Python. I usually think about attributes as nouns that belong to an object. For example, “the student has two eyes”. But in Python, an  AttributeError: module 'torch.jit' has no attribute 'unused' #26608. Closed haruiz opened this issue Sep 21, 2019 · 12 comments Closed

This happens because the scipy module doesn't have any attribute named sparse . That attribute only gets defined when you import  AttributeError: module 'asyncio' has no attribute 'coroutine' (Python 3.6.4) -1 AttributeError: Why does the module first imported not register as an attribute?

AttributeError: module 'sortedcontainers' has no attribute 'SortedKeyList' #57. Open. KittyTechnoProgrammer opened this issue on Dec 16,  Concrete exceptions¶ The following exceptions are the exceptions that are usually raised. exception AssertionError¶ Raised when an assert statement fails. exception AttributeError¶ Raised when an attribute reference (see Attribute references) or assignment fails.

Comments
  • You're trying to access a variable named ai_settings.screen_width in the run_game() function, but the name is misspelled. That said, the line self.ai.screen_width = 1200 line in Settings.__init__() should be giving you a AttributeError: 'Settings' object has no attribute 'ai'—but you're not asking about that, so I suspect the code you've posted isn't what you're running. Am I putting that nicely enough for you?
  • I have updated the code.self.ai.screen_width = 1200 should actually be this self.screen_width = 1200 And you are correct in thinking that code was not being ran. I was trying different things and pasted the wrong snip. How can you tell that ai_settings.screen_width is misspelled? Also, I originally meant to ask about the Attribute error. Thank you for taking the time to look over my code.
  • Jay: Actually I think it should be changed to self.ai_screen_width = 1200 so it matches the other place—although exactly what name you give it isn't terribly important as long as you use it consistently and it's a simple identifier name (i.e. one that doesn't contain an embedded .). I noticed the problem when first trying to run your code to reproduce the problem and got an exception which was different from what's in your question.
  • I am not sure I follow you. Am I not using ai.screen_width ?
  • In your code you're using ai_settings.screen_width but you should use ai_settings.ai.screen_width.
  • I used your settings but I am still getting an attribute error. AttributeError: 'Settings' object has no attribute 'ai'