Python类函数不能正常工作

2024-09-30 08:38:13 发布

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

下面是我用PyCharm编写的代码。我不明白为什么它不能正常运行并打印出“Chuck”。当我在PyCharm中运行代码时,我得到以下消息“processfinishwithexitcode0”。我正在努力学习Python并跟随YouTube播放列表,但我一直被困在这里。提前谢谢你!在

class class_name:
def createName(self, name):
    self.name=name
def display_name(self):
    return self.name
def saying(self):
    print("hello %s" % self.name)

first=class_name()
second=class_name()
first.createName('Chuck')
second.createName('tony')
first.display_name()

Tags: 代码nameself消息youtubedefdisplay播放列表
2条回答

你好Chuck0185

在启动Python3之前,请先阅读此最佳网站,
1https://www.tutorialspoint.com/python3/
2https://docs.python.org/3/tutorial/
三。https://learnpythonthehardway.org/python3/

python 2和python 3的区别,
1http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html
2https://www.quora.com/What-are-the-major-differences-between-Python-2-and-Python-3

Pycharm工具知识,用于最佳网站,
1https://www.jetbrains.com/help/pycharm/2017.1/creating-and-running-your-first-python-project.html

尝试此代码, 使用Python2:

#!/usr/bin/python

# Using python 2
class class_name:
    def createName(self, name):
        self.name=name
    def display_name(self):
        return self.name
    def saying(self):
        print("hello %s" % self.name)

first=class_name()
second=class_name()

first.createName('Chuck')
second.createName('tony')

print "First Name is: %s" % first.display_name()
print "Second Name is: %s" % second.display_name()

first.saying()
second.saying()

使用Python3:

^{pr2}$

我希望我的回答对你有用。在

试试这个:

class class_name:
    def __init__(self, name):
        self.name = name
    def display_name(self):
        return self.name
    def saying(self):
        print("hello %s" % self.name)

first = class_name('Chuck')
second = class_name('Tony')
print(first.display_name()) #Chuck

作为first.display_名称()返回一个字符串,您需要使用print在终端中显示值。在

相关问题 更多 >

    热门问题