如何分成几个部分

时间:2018-05-23 08:06:43

标签: python python-3.x jupyter

你能帮我吗? 我正在为初学者阅读一本Python书籍,我想在一个笔记本文件中测试我学到的所有新东西。 (我用Jupyter)

我的#comment然后在示例下面。当我忘记了什么时,我的目标是使用这款笔记本。

如何划分每一章,使其独立,不会影响同一个笔记本文件中的其他练习? 我希望这个问题有道理。

亲切的问候, NESH

1 个答案:

答案 0 :(得分:0)

您可以将每个章节分解为类,这样(假设您的语法正确),每个类都不会影响其他类。例如:

#! /usr/bin/env python3

class chapter1:
    def run(self):

        #in this chapter I learned how to print to the console:
        print ("Hello world")

class chapter2:
    def run(self):

        #in this chapter I learned how to print variables
        a = 1;
        b = 2;
        print ("%i + %i = %i" % (a,b,a+b))

#here is where you can run each chapter if you want
c1 = chapter1()
c1.run() # this will output "Hello world"

c2 = chapter2()
c2.run() # this will output "1 + 2 = 3"

编辑:对不起,当我回答这个问题时,我没有意识到Jupyter是什么。这个答案的目的是将所有笔记保存在一个文本文件中。

相关问题