如何将函数结果作为类的参数?

时间:2019-04-10 07:29:36

标签: python python-3.x

我有一个程序,该程序将询问用户有关参数的信息并将其传递给类,但我找不到方法。我尝试将函数作为参数,但没有用。

class Exam:
    def __init__(self, day, importance):
        self.day = day
    self.importance = importance


days = [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
]


def is_important():
    for day in days:
        answer = input("Ìs " + day + " Important")
        if answer == "True":
            return True
        elif answer == "False":
            return False


is_important() 

mon = Exam("Monday",)
tue = Exam("Tuesday", )
wed = Exam("Wednesday", )
thu = Exam("Thursday", )
fri = Exam("Friday", )
sat = Exam("Saturday", )
sun = Exam("Sunday", )

print(mon.importance)
print(tue.importance)
print(wed.importance)
print(thu.importance)
print(fri.importance)
print(sat.importance)
print(sun.importance)

4 个答案:

答案 0 :(得分:0)

Exam类将定义如下。在这里,我们传递了两个参数并将其分配给类成员,还定义了./gradlew copyApk or gradle copyApk 类,并分配了重要性变量

is_important

然后,您可以使用单个对象分配重要性并进行打印

class Exam:
    def __init__(self, day, importance):
        self.day = day
        self.importance = importance

    def is_important(self):

        answer = input("Ìs " + day + " Important")
        if answer == "True":
            self.importance = True
        else:
            self.importance = False

或者循环播放几天,然后每天拨打mon = Exam("Monday", "True") print(mon.day) print(mon.importance) tue = Exam("Tueday", "False") print(tue.day) print(tue.importance) #Monday #True #Tueday #False

is_important

输出将为

days = [
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday",
]

exam_days = []
for day in days:
    exam_day = Exam(day)
    exam_day.is_important()
    exam_days.append(exam_day)

for exam_day in exam_days:
    print(exam_day.day)
    print(exam_day.importance)

答案 1 :(得分:0)

is_important()应该以天为参数。然后您可以在不同的日期进行调用,并将结果传递给全班。

def is_important(day):
    answer = input("Ìs " + day + " Important")
    return answer.lower() == "true"

mon = Exam("Monday", is_important("Monday");
tue = Exam("Tueday", is_important("Tuesday");
...

每天最好使用字典而不是单独使用变量。然后,您可以执行以下操作:

exam_days = {day: Exam(day, is_important(day)) for day in days}

答案 2 :(得分:0)

实现此目的的最佳方法是使is_important为类方法。这更加Python化,并且在问题和工作日之间建立了明确的联系。从本质上讲,它是面向对象的,因为重要性和要求都与检查对象明确相关。就像这样:

class Exam:
    def __init__(self, day):
        self.day = day
        self.is_important()

    def is_important(self):
        answer = input("Ìs " + self.day + " Important? ")
        if answer == "Yes":
            self.importance = True
        elif answer == "No":
            self.importance = False

然后只需循环初始化每天的Exam对象,然后将结果添加到字典中。字典的优势在于,您可以初始化多个变量,而不必键入额外的行,因为它们具有明确的变量名称。如果您有许多相同类型的对象,则字典的键是比普通变量名更好的选择。


days = ["Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday"]

my_week = {}

for day in days:
    my_week.update({day: Exam(day)})
    print my_week[day].importance

答案 3 :(得分:0)

在考虑问题时,如果您希望携带Exam对象具有改变重要性的能力,那么这可能是您要考虑的方法:

any