'Python module execution not being procedural
I am making a python module to edit a BrainFuck script and make it easy to program. (This is just for fun.) I have run into a problem where the functions are executing out of order and I have no idea how to fix it.
__init__.py:
#main
def init():
f=open("main.bf","w")
f.close
f=open("BrainF/cell.txt","w")
f.write("0")
f.close
class cell:
def goto(num):
f=open("BrainF/cell.txt","r")
current_cell=int(f.read())
f.close
y=current_cell-num
if num >> current_cell:
f=open("main.bf","a")
x="a"*(num-current_cell)
for i in x:
f.write(">")
f.close()
if num << current_cell:
f=open("main.bf","a")
x="a"*abs(y)
for i in x:
f.write("<")
f.close()
f=open("BrainF/cell.txt","w")
f.write(str(num))
f.close
def add(num):
f=open("main.bf","a")
x="a"*num
for i in x:
f.write("+")
f.close()
def subtract(num):
f=open("main.bf","a")
x="a"*num
for i in x:
f.write("-")
f.close()
main.py:
import BrainF as bf
bf.init()
bf.cell.goto(2)
bf.cell.add(3)
bf.cell.goto(0)
file tree:
main.bf
main.py
BrainF
--__pycache__
--__init__.py
--cell.txt
When I run main.py with the command, python main.py
it writes to main.bf. The problem is that it should execute in the correct order and main.bf would be >>+++<<
but instead, it does not and the actual contents is >><<+++
.
Is there any way to fix this so that the functions execute in order?
Link to repl to try for yourself: https://replit.com/@SamuelAndrews-Pirona/Python-BrainF
Solution 1:[1]
I fixed it. The problem was me not returning after finishing the function, and using >> instead of >. I fixed it and it works on the repl as well.
If you want, you can check in on the repl and use it as a easy python to BrainFuck translator!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | TryToDoIt |