Search code examples
pythonmit-scratch

Python in Python


Scratch is made from flash. It isn't coding. But python is (or at least I think it is ). So I was wondering if it was possible to

a) Make a coding language in python for the user to make

b) Make the User's code executable.

P.S. Is there a way for variables when changed by the user, becomes changed in your code? I need to know this for something.


Solution

  • def Write(linenumber,file,content):
            ile=open(file,'r')
            filer=ile.readlines()
            ile.close
            file1=open(file,'w')
            linenumber=linenumber-1
            loop=-1
            for elem in filer:
                loop=loop+1
                if loop!=linenumber:
                    file1.write(elem)
                else:
                    file1.write(content)
    def Read(linenumber,file):
              ile=open(file,'r')
              lines=ile.readlines()
              ile.close()
              return lines[linenumber-1]
    

    For your second question, you could do something like this to write to the same file, if you want the change to be permanent. Just use the functions above to change the variable permanently. You could also write to a separate file which would be more ideal with the functions.