没有得到预期的结果

2024-06-28 10:58:55 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么我不能得到结果(肯德基是美国餐馆)?我怎样才能改变它?我符合要求吗?你知道吗

class Restaurant:  
    __name=""
    __cuisine=""


    def __init__(self,name,cuisine):
        self.__name=name
        self.__cuisine=cuisine

    def describe_restaurant(self):
             print(self.__name,  " is a ",self.__cuisine ," restarurant.")

    def open_restaurant(self):
             print(self.__name ," is open.")


def test():
    p=Restaurant("KFC","American")
    print(p.describe_restaurant)

Tags: nameselfinitisdefopenrestaurantclass
1条回答
网友
1楼 · 发布于 2024-06-28 10:58:55

describe_restaurant是一个函数。当你写作的时候

print(p.describe_restaurant)

你得到的是一个函数的字符串表示。但是,您希望调用这个函数,让它执行,并打印其返回值。为此,请添加括号:

p.describe_restaurant()

此外,确保实际调用test方法,如下所示:

test()

相关问题 更多 >