skip to Main Content

I created a little program as part of my learning experience using python crash course and the code worked pretty well yesterday. But now that I woke up and tried to launch the thing it refuses to do anything and says that "self" is not defined. I honestly have no idea why it happens and would very much like to know exactly what causes error and where I mistaken. Sorry if the question format is wrong and thanks in advance for any help.

import json

class Save_user:
    """Greet the user if the username presents."""
    """Ask the name otherwise."""

    def __init__(self):
        """Sets username; Calls greet_user()"""
        self.file_path = 'username.json'
        self.greet_user()

    def get_stored_username(self):
        """Get the username if stored."""
        try:
            with open(self.file_path) as f:
                self.username = json.load(f)
        except FileNotFoundError:
            return None
        else:
            return self.username

    def greet_user(self):
        """Choose greet the user or store the username."""
        self.get_stored_username()
        if self.username:
            self.if_same_user()
        else:
            self.store_name()

    def store_name(self):
        """Store username."""
        self.username = input("Enter your username: ")
        with open(self.file_path, 'w') as f:
            json.dump(self.username, f)
        print("Great! We'll greet you next time!")

    def if_same_user(self):
        """Check if the same user."""
        print(f"Are you {self.username}?")
        while True:
            response = input("Please, Enter 'yes' or 'no': n")
            response = response.lower().strip()
            if response == 'yes' or response == 'y':
                print(f"Welcome back, {self.username}!")
                break
            elif response == 'no' or response == 'n':
                self.store_name()
                break

useame = Save_user()

The program should asks the user’s name if the json file exists and create the file and store the name otherwise. I tried to set username to 0 in __init__ module and I could launch the thing with text editor and .py format, visual studio, however is giving me an error. Again, thanks in advance for any help!

UPD TraceBack:

    Traceback (most recent call last):
  File "c:UsersWindows 10Desktoppython_workNew foldernew.py", line 50, in <module>  
    username = Save_user()
               ^^^^^^^^^^^
  File "c:UsersWindows 10Desktoppython_workNew foldernew.py", line 10, in __init__  
    self.greet_user()
  File "c:UsersWindows 10Desktoppython_workNew foldernew.py", line 25, in greet_user
    if self.username:
       ^^^^^^^^^^^^^
AttributeError: 'Save_user' object has no attribute 'username'

3

Answers


  1. The problem arises when the file is not found: you return None, but don’t actually asign it to self.username. So when you do if self.username, an error will rise. I tweaked two lines of your code, here are the functions to change:

        def get_stored_username(self):
            """Get the username if stored."""
            try:
                with open(self.file_path) as f:
                    username = json.load(f)
                    return username
            except FileNotFoundError:
                return None
    
    
        def greet_user(self):
            """Choose greet the user or store the username."""
            self.username = self.get_stored_username()
            if self.username:
                self.if_same_user()
            else:
                self.store_name()
    
    Login or Signup to reply.
  2. The traceback is very helpful here. The problem is that __init__ calls self.greet_user(), which expects self.username to exist. But self.username does not exist yet.

    class Save_user:
        def __init__(self):
            """Sets username; Calls greet_user()"""
            self.file_path = 'username.json'
            self.greet_user()
            # NOTE: there is no self.username here
    
        # ...
    
        def greet_user(self):
            """Choose greet the user or store the username."""
            self.get_stored_username() # May fail to set self.username
            if self.username: # This will cause an error
                self.if_same_user()
            else:
                self.store_name()
    

    Typically, attempting to instantiate an object of this class when there is no file username.json will cause an AttributeError that self.username is not one of the attributes. One way to solve this would be to add a sentinel or default username, such as

        # ...
        def __init__(self):
            self.file_path = 'username.json'
            self.username = 'default'
            self.greet_user()
        # ...
    

    A different default name (especially one that is not allowed to be set by standard means) would be a better choice.

    Login or Signup to reply.
  3. Your issue is in the method get_stored_username

    you are returning None instead of defining the property self.username

        def get_stored_username(self):
            """Get the username if stored."""
            try:
                with open(self.file_path) as f:
                    self.username = json.load(f)
            except FileNotFoundError:
                return None # <- error
            else: # <- not necessary
                return self.username  # <- not necessary
    

    Define the property:

        ...
            except FileNotFoundError:
                self.username = None
    

    complete method:

        def get_stored_username(self):
            """Get the username if stored."""
            try:
                with open(self.file_path) as f:
                    self.username = json.load(f)
            except FileNotFoundError:
                self.username = None
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search