0

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.

Captain Jack Sparrow
  • 971
  • 1
  • 11
  • 28
  • So the intent is that you provide the *text* `felix` to the program, and the program correspondingly proceeds to work with the *variable* `felix`? What do you want to happen if the input isn't an existing variable? What do you want to happen if it's some other variable that isn't a Patient? – Karl Knechtel Apr 02 '20 at 01:30
  • Does https://stackoverflow.com/questions/11864926/python-access-variable-from-string help? – Karl Knechtel Apr 02 '20 at 01:34
  • The aim is that it would output whatever the age of the input for request =input is.. – ChrisDotPy Apr 02 '20 at 01:37
  • 1
    So currently its just a short code because I was using a totally different one with dictionaries and had it working with request[age] as that was right for dictionaries.. Now I'm trying to use classes as I can input more attributes. I put some exception handling in for inputs that weren't a key in the dictionary that prompted for a correct key and had a block to list all (keys) patients.. – ChrisDotPy Apr 02 '20 at 01:40

2 Answers2

3

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.

Dave Kielpinski
  • 1,152
  • 1
  • 9
  • 20
  • Yeah I just noticed a second ago that it was in there as str(input()) so I changed str for Patient and to no avail.. I'll figure out how to do it eventually, hopefully – ChrisDotPy Apr 02 '20 at 01:31
  • You've defined `Patient` as needing both `color` and `age` inputs. Simply replacing `str` by `Patient` is just going to give you an error. I think you have some misconceptions about how Python objects work, I'd suggest reading up on how to define and use classes in Python. – Dave Kielpinski Apr 02 '20 at 01:33
  • The result of `input()` is a string. Doing `str(input())` has no use; doing `Patient(input())` tries to *create a new Patient* passing that string to the `__init__`. So you get a different error message because `Patient` isn't designed to be constructed that way. – Karl Knechtel Apr 02 '20 at 01:33
  • 1
    best of luck, you'll get there :) – Dave Kielpinski Apr 02 '20 at 01:36
  • since i see you're a new user: if you find an answer satisfactory, it's considered polite to "accept" it. kind of keeps people motivated to keep answering :) – Dave Kielpinski Apr 02 '20 at 05:12
0

I reformatted it a bit, but here's what I got! This ran well for me. If you like it and it works for you, please feel free to take and edit it.

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


felix = Patient('Felix', 'White British', 21)

verified_action = False

while not verified_action:
    user_in = input('What would you like to do?\n').lower()
    if user_in == "find patient age":
        verified_action = True
    else:
        print(f'{user_in} is not a valid input, please try again.')

picked_patient = False

while not picked_patient:
    request = input("Name of patient: ").lower()
    if request == 'felix':
        name = felix.name
        age = felix.age
        color = felix.color
        picked_patient = True
    else:
        print(f'{request} not recognized, please try again.')

print(f"{name}'s age is {age} years.")

You could continue to add elif statements as you continue to add patients, and just set their respective variables to:

name = timothy.name
# etc.

However, this would become cumbersome over time, so perhaps storing this data in something like a Pandas DataFrame and then accessing that DataFrame for the information would be a lot quicker. And the hospital, or whomever would use this application, would find it much easier to not only retrieve large quantities of information, but also add it in very easily, without having to write in the extra code.

I hope this helps!

T.J. Kolberg
  • 47
  • 1
  • 6