python jupyter magic %% writefile返回SyntaxError:语法无效

Kar*_*ang 0 python writefile jupyter jupyter-notebook

# [ ] The following program asks the user for a circle radius then display the area and circumference
# Modify the program so it only displays the information when executed directly
# The program should not display anything if it is imported as a module 


%%writefile main_script.py

def main(): 
    from math import pi

    def circle_area(r):
        return pi * (r ** 2)

    def circle_circumference(r):
        return  2 * pi * r

    radius = float(input("Enter radius: "))
    print("Area =", circle_area(radius))
    print("Circumference =", circle_circumference(radius))

if __name__="__main__":
    main()


----------
  File "<ipython-input-3-70ba6a5d5e98>", line 6
    %%writefile main_script.py
    ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我该如何解决??

它是一个练习,但是我不明白这个系统命令是如何工作的,您能解释一下吗?

忽略:对于单词的要求忽略:对于单词的要求

Jus*_*tin 5

这对您不起作用,因为您在笔记本单元格中代码的第一行中有注释。如果将注释移动到magic命令下,它将写入文件。

%%writefile main_script.py

# Add all of your comments here after the magic command

def main(): 
    def add_my_code(here):
        return here

if __name__="__main__":
    main()
Run Code Online (Sandbox Code Playgroud)