简体   繁体   English

在python3中获取AttributeError

[英]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.任何帮助将不胜感激,我知道这可能看起来像一个愚蠢的问题。

request is not a instance of the Patient class. request不是Patient类的实例。 You've used input() to retrieve the user input, converted it to a string using str() , and set request to equal the result.您已使用input()检索用户输入,使用str()将其转换为字符串,并将request设置为等于结果。 So request has no age attribute, since it's a string not a Patient .所以request没有age属性,因为它是一个字符串而不是Patient

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:您可以在继续添加患者时继续添加 elif 语句,只需将它们各自的变量设置为:

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.但是,随着时间的推移,这会变得很麻烦,因此也许将这些数据存储在 Pandas DataFrame 之类的东西中,然后访问该 DataFrame 以获取信息会快得多。 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!我希望这有帮助! 🤙🏼 🤙🏼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM