如何更改str()类型以使我的程序知道我在谈论对象?

时间:2018-04-19 11:15:27

标签: string python-3.x types format

我即将建立一个有趣的数据库。但是我已经陷入困境。

我正在尝试使我的搜索功能更加优化。所以我想这样做:

错误就像我记得的那样。属性错误' str'对象没有属性' print_info'

if password == "1211":
    person = input("Who are you searching for? ")
    if person == "Everyone":
        print("Searching for everyone...")
        info = input("What do you want to know about everyone? ")
        if info != "Job" and info != "Salary" and info != "Age" and info != "Email" and info != "Sex" and info != "Everything":
            print("No info was found")
        else:
            for key, value in employeeDict.items():
                print("")
                key.print_info(str(info))
    else: 
        print("Searching for", person + "...")
        info = input("What do you want to know about " + str(person) + "? ")
        if info != "Job" and info != "Salary" and info != "Age" and info != "Email" and info != "Sex" and info != "Everything":
            print("No info was found")
        else:
            person.print_info(str(info))
else:
    print("Access denied")

我的"每个人"搜索功能工作正常,但我需要找到一个明确的员工的功能给了我一个属性错误,因为" person"变量被格式化为字符串。我想知道是否可以格式化person变量以使python知道它是一个对象。

我的所有代码都是这样的。

import datetime
from employeeVariables import *
from employeeClass import *

August = Employee("August", "Lyngstrand", "A.V. Fornitz Lyngstrand", "CEO", "75%", str(augustBirthDate) + " " + "(" + str(int(augustAge.days/365.25)) + " years and " + str(int(augustAge.days % 365.25)) + " days)", "1_" + str(augustBirthDate), "Male") 
Rasmus = Employee("Rasmus", "Preisler", "Rasmus F. Preisler", "Co. Founder", "25%", str(rasmusBirthDate) + " " + "(" + str(int(rasmusAge.days/365.25)) + " years and " + str(int(rasmusAge.days % 365.25)) + " days)", "1_" + str(rasmusBirthDate), "Male")
Helle = Employee("Helle", "Rasmussen", "Helle Rasmussen", "Investor", "0%", str(helleBirthDate) + " " + "(" + str(int(helleAge.days/365.25)) + " years and " + str(int(helleAge.days % 365.25)) + " days)", "2_" + str(helleBirthDate), "Female")
Lars = Employee("Lars", "Lyngstrand", "Lars L. Lyngstrand", "Investor", "0%", str(larsBirthDate) + " " + "(" + str(int(larsAge.days/365.25)) + " years and " + str(int(larsAge.days % 365.25)) + " days)", "1_" + str(larsBirthDate), "Male")

employeeDict = {August: August.ID, Rasmus: Rasmus.ID, Helle: Helle.ID, Lars: Lars.ID}

password = input('''Enter password to access the database
Password: ''')

if password == "1211":
    person = input("Who are you searching for? ")
    if person == "Everyone":
        print("Searching for everyone...")
        info = input("What do you want to know about everyone? ")
        if info != "Job" and info != "Salary" and info != "Age" and info != "Email" and info != "Sex" and info != "Everything":
            print("No info was found")
        else:
            for key, value in employeeDict.items():
                print("")
                key.print_info(info)
    else: 
        print("Searching for", person + "...")
        info = input("What do you want to know about " + str(person) + "? ")
        if info != "Job" and info != "Salary" and info != "Age" and info != "Email" and info != "Sex" and info != "Everything":
            print("No info was found")
        else:
            for key in employeeDict.items():
                if key == person: 
                    key.print_info(info)
                else:
                    print("The employee doesn't exist in the database")
else:
    print("Access denied")

提前感谢:)

2 个答案:

答案 0 :(得分:1)

您是否尝试使用词典中的所有键进行for循环并说出

If key == person
    Key.print__info(info)

我认为这会奏效。

答案 1 :(得分:1)

所以我找到了问题的解决方案。我不确定它是否是最好的,但它现在可以正常工作。

我发现我并不需要我的dict(employeeDict)来获得值Employee.ID。相反,我将密钥更改为字符串,我的值为Employee。我现在可以将我的输入字符串与键匹配,并使其打印该键的值。

帮助