(Sorry for any mistakes, this is my first post after hours of searching for a solution!!)
I have stored function names and its arguments in a txt file, and asked to call functions that carry out certain commands.
What I have done so far:
def main():
global Pen
filename = input("Please enter the name of the file: ")
plt.axis('square')
plt.axis([-400, 400, -400, 400])
Pen = (0, 0, False, 0)
file = open(filename)
commands = []
for data in file:
data = data.split(',')
cs = data[0]
ca = (data[1].rstrip('\n'))
command = (cs, ca)
commands.append(command)
print(commands)
for i in commands:
i[0](i[1])
this is giving me a typeError: 'str' object is not callable.
How do I call the functions using a string? is there any other way to do this? (The worksheet asks me to read commands stored in an txt instruction file)
All the code for context:
from matplotlib import pyplot as plt
import math
from math import *
Pen = (0, 0, False, 0)
def main():
global Pen
filename = input("Please enter the name of the file: ")
plt.axis('square')
plt.axis([-400, 400, -400, 400])
Pen = (0, 0, False, 0)
file = open(filename)
commands = []
for data in file:
data = data.split(',')
cs = data[0]
ca = (data[1].rstrip('\n'))
command = (cs, ca)
commands.append(command)
print(commands)
for i in commands:
i[0](i[1])
def rotate(angle):
global Pen
Pen = list(Pen)
Pen[3] = Pen[3] - angle
Pen = tuple(Pen)
def forward(distance):
global Pen
Pen = list(Pen)
x = [Pen[0]]
y = [Pen[1]]
a = Pen[0] + (cos(radians(Pen[3])) * distance)
b = Pen[1] + (sin(radians(Pen[3])) * distance)
Pen[0] = a
Pen[1] = b
Pen = tuple(Pen)
if Pen[2]:
x.append(a)
y.append(b)
plt.plot(x, y, 'b-')
def pen(state):
global Pen
Pen = list(Pen)
Pen[2] = state
Pen = tuple(Pen)
main()
print(Pen)
plt.show()