Search code examples
pythonpython-3.xclassattributeerror

Getting an AttributeError in python3


So, I'm new to this programming stuff and I've been working on a few little projects to try and consolidate what I'm learning. I've got this patient database UI I've been making and it was using dictionaries. I've now learnt to use classes instead so I can include more data than just one variable to a key but when trying to reference an attribute for output its giving me Attribute Error.. its almost the same code I used before but with the .age included.. Anyone able to help me out and just explain why I cant use the "request" line that I previously used with dictionaries and maybe suggest a way around it? error code image

class Patient:
    def __init__(patient, color, age):
        patient.color = color
        patient.age = age

felix = Patient ("White_British", 21)
print(felix.color)

while True:
    print ("What would you like to do?")
    
    usin = str(input("  "))
    
# Find Patient Age Function
    
    if usin == "find patient age":
        try: 
            request = str(input("Name of patient: "))
            print (request + "'s age is " + request.age + " Years"

Any help would be greatly appreciated, I know it probably seems like a stupid question.


Solution

  • request is not a instance of the Patient class. You've used input() to retrieve the user input, converted it to a string using str(), and set request to equal the result. So request has no age attribute, since it's a string not a Patient.