Python类函数无法正常工作

时间:2017-06-13 00:05:05

标签: python pycharm

我在PyCharm中编写了以下代码。我无法弄清楚为什么它不能正常运行并打印出来#Chuck"。当我在PyCharm中运行代码时,我得到以下消息"处理完成,退出代码为0"。我正在尝试学习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()

2 个答案:

答案 0 :(得分:4)

试试这个:

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_name()返回一个字符串时,您需要使用print来显示终端中的值。

答案 1 :(得分:-1)

你好Chuck0185

在开始使用Python 3之前首先阅读这个最好的网站,
1. https://www.tutorialspoint.com/python3/
2. https://docs.python.org/3/tutorial/
3. https://learnpythonthehardway.org/python3/

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

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

试试此代码, 使用python2:

CellValueChanged

使用python3:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
  if (dataGridView1.Columns[e.ColumnIndex].Name == "DOB") {
    if (!String.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells["DOB"].Value.ToString())) {
      DateTime newDOB = (DateTime)dataGridView1.Rows[e.RowIndex].Cells["DOB"].Value;
      dataGridView1.Rows[e.RowIndex].Cells["Age"].Value = getAge(newDOB); ;
    } else {
      dataGridView1.Rows[e.RowIndex].Cells["Age"].Value = "";
    }
  }
}

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