0

Calculator Language is what the problem is called and I am to code it in python. The coding part is done but I am having trouble while reading the input file.

So the input file looks like this :

A = B = 4
C = (D = 2)*_2
#

What i would like to do is to read each character, line by line ( each line is an expression and has to be calculated), characters as characters and integers as integers, since I push them into stacks. There are two stacks one for the characters and numbers and the other for the operators.

Anyway this is what I have done with the input so far :

#!/usr/bin/python

a = open("testinput1.txt","r+")
wordList = [line.strip() for line in a];


print wordList[1]

And what i get is :

C = (D = 2)*_2

Also the end of file is reached when the file reader hits #.

Any sort of help or suggestions are welcome.

5
  • I'm confused here. What isn't working? Commented Jun 7, 2013 at 6:01
  • Because I still do not have the individual characters. All i have as the wordList[1] is the expression. I need the C,=,(,D individually to operate on them Commented Jun 7, 2013 at 6:04
  • Have you tried iterating over wordList[1]? Commented Jun 7, 2013 at 6:07
  • Is that possible? Lemme just check. Gimme five. Commented Jun 7, 2013 at 6:08
  • lst = list(wordlist[1]) Commented Jun 7, 2013 at 6:24

2 Answers 2

1

wordList is list of lines, each element is line (stripped one, without \n') You should split each line, to get its tokens. Then for each token check if it is string or integer (using isdigit for example).

Sign up to request clarification or add additional context in comments.

Comments

1

Now that your wordlist[0] contains your first statement, In python each and every string can be indexed directly without creating a seperate list for it.

for example: if wordlist[0] contains "c=a+b" , wordlist[0][0] will directly give you 'c'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.