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
The problem arises when the file is not found: you return None, but don’t actually asign it to
self.username
. So when you doif self.username
, an error will rise. I tweaked two lines of your code, here are the functions to change:The traceback is very helpful here. The problem is that
__init__
callsself.greet_user()
, which expectsself.username
to exist. Butself.username
does not exist yet.Typically, attempting to instantiate an object of this class when there is no file
username.json
will cause anAttributeError
thatself.username
is not one of the attributes. One way to solve this would be to add a sentinel or default username, such asA different default name (especially one that is not allowed to be set by standard means) would be a better choice.
Your issue is in the method
get_stored_username
you are returning
None
instead of defining the propertyself.username
Define the property:
complete method: