python - 类函数输出正确但方法错误

标签 python python-3.x

我的作业问题要求我使用预定的类“Person”编写以下函数,它运行 5 个测试,一个代码分析器确保它按照他们想要的方式完成,还有 4 个实际示例,我的代码通过了实际示例,但是分析仪让我失败了。

这就是问题:

使用 Person 类,编写一个函数 print_friend_info(person),它接受 Person 类型的单个参数,并且:

打印出他们的名字

打印出他们的年龄

如果此人有任何 friend ,则打印“Friends with {name}”

class Person(object):
    def __init__(self, name, age, gender):
        self._name = name
        self._age = age
        self._gender = gender
        self._friend = None

    def __eq__(self, person):
        return str(self) == str(person)

    def __str__(self):
        if self._gender == 'M':
            title = 'Mr'
        elif self._gender == 'F':
            title = 'Miss'
        else:
            title = 'Ms'

        return title + ' ' + self._name + ' ' + str(self._age)

    def __repr__(self):
        return 'Person: ' + str(self)

    def get_name(self):
        """
        (str) Return the name

        """
        return self._name

    def get_age(self):
        """
        (int) Return the age

        """
        return self._age

    def get_gender(self):
        """
        (str) Return the gender

        """
        return self._gender

    def set_friend(self, friend):
        self._friend = friend

    def get_friend(self):
        """
        (Person) Return the friend

        """
        return self._friend

def print_friend_info(person):
    print(person._name)
    print(person._age)
    if person.get_friend() != None:
        print("Friends with {}".format(person._friend._name))

所有测试均通过,包括 print_friend_info 测试,但此测试输出以下错误:

----- Analysis Errors -----

You need to print the person's name in print_friend_info

You need to print the person's age in print_friend_info

You need to print the name of the person's friend in print_friend_info

最佳答案

我已经尝试过你的代码,它适用于我的Python3.6.6。我有一些可能会出现问题的建议。首先,您不应该使用类的 protected 属性(下划线变量)。您有 getter 方法来获取对象的数据,因此请使用它们。在我看来,输出可以满足您在问题中提到的测试。

更改了部分代码:

def print_friend_info(person):
    print("Name: {}".format(person.get_name()))
    print("Age: {}".format(person.get_age()))
    if person.get_friend():  # You don't need to use "!= None". A simple if is enough.
        print("Friends with {}".format(person.get_friend().get_name())
    print("\n")


joe = Person("Joe", 42, "Male")
print_friend_info(joe)
mary = Person("Mary", 55, "Female")
print_friend_info(mary)
joe.set_friend(mary)
print_friend_info(joe)

输出:

>>> python3 test.py 
Name: Joe
Age: 42


Name: Mary
Age: 55


Name: Joe
Age: 42
Friends with Mary

关于python - 类函数输出正确但方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57518869/

相关文章:

python - 更新 tkinter Label 以在我的 python GUI 上显示文本文件,一次一行

python - 用于映射和复制数据的 ctypes

python - 应如何将指标添加到多头 TensorFlow 估计器中?

python - ImportError : No module named distutils

python - 自动完成什么都不做。怎么了?

python - 在 Python 3.x 中获取 map() 以返回列表

python - 给定两个非零整数,如果恰好有一个为正数则打印 "YES"否则打印 "NO"(Python)

python - Pytube 库 - 尝试访问视频数据时收到 "pytube.exceptions.RegexMatchError: regex pattern"错误

python-3.x - 表现相同名称的多个步骤

python-3.x - 在dockerfile中缓存PIP包