在学习python的过程中,我只是想想出一些可以编写的程序。然后我想,是否可以编写一个程序,接受用户的输入,然后将其作为代码写入.py文件。
如果我们要运行.py文件,我们应该得到我们输入的输出,就像print("Hello World")
的工作方式一样。
有什么想法可以写出来吗?
编辑:我还在学习,所以请像我只知道基本的打印代码和一些数学运算一样解释:(
您可以使用:
file = open("test.py", "w") # Open a file for writing(overwrites previous files) and puts it in the file variable
file.write("print('Hello world')") # .write() adds content of print('Hello world')
file.close() # Close the file
您可以使用:
file = open("test.py", "w")
file.write("print('Hello world')n")
file.write("print('Hello world2')n")
file.close()
对于多行代码