鱼C论坛

 找回密码
 立即注册
查看: 874|回复: 3

[已解决]为什么输出结果中跟着None ????

[复制链接]
发表于 2019-6-14 10:53:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

class Restaurant:
    def __init__(self, rest_name, cuisine_type):
        self.rest_name = rest_name
        self.cuisine_type = cuisine_type

    def describe_resturant(self):
        print('这家饭店叫' + self.rest_name + '是一家'+ self.cuisine_type + '.')

my_restaurant = Restaurant('《我家》', '中餐馆')
print(my_restaurant.describe_resturant())


运行后最后为什么有None:
这家饭店叫《我家》是一家中餐馆.
None
最佳答案
2019-6-14 10:58:49
  1. class Restaurant:
  2.     def __init__(self, rest_name, cuisine_type):
  3.         self.rest_name = rest_name
  4.         self.cuisine_type = cuisine_type

  5.     def describe_resturant(self):
  6.         print('这家饭店叫' + self.rest_name + '是一家'+ self.cuisine_type + '.')

  7. my_restaurant = Restaurant('《我家》', '中餐馆')
  8. my_restaurant.describe_resturant()
复制代码


直接call 函数就可以了,没必要去再次print,因为print是没有返回值的,你call了一次就已经出结果了,再去打印一个没有返回值的东西就是None了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-14 10:58:49 | 显示全部楼层    本楼为最佳答案   
  1. class Restaurant:
  2.     def __init__(self, rest_name, cuisine_type):
  3.         self.rest_name = rest_name
  4.         self.cuisine_type = cuisine_type

  5.     def describe_resturant(self):
  6.         print('这家饭店叫' + self.rest_name + '是一家'+ self.cuisine_type + '.')

  7. my_restaurant = Restaurant('《我家》', '中餐馆')
  8. my_restaurant.describe_resturant()
复制代码


直接call 函数就可以了,没必要去再次print,因为print是没有返回值的,你call了一次就已经出结果了,再去打印一个没有返回值的东西就是None了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-6-14 11:29:01 | 显示全部楼层
Seawolf 发表于 2019-6-14 10:58
直接call 函数就可以了,没必要去再次print,因为print是没有返回值的,你call了一次就已经出结果了, ...

非常感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-20 03:51:30 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 13:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表