Note that there are some explanatory texts on larger screens.

plurals
  1. POint() argument must be a string or a number
    text
    copied!<p>I got an error. I did quick googling and it did not help me well.</p> <p>I added the whole code, well kind of whole code. request from a user.</p> <pre><code>from derp_node import * ############################################################################## # parse ############################################################################## def parse(tokens, i = 0): """parse: tuple(String) * int -&gt; (Node, int) From an infix stream of tokens, and the current index into the token stream, construct and return the tree, as a collection of Nodes, that represent the expression. NOTE: YOU ARE NOT ALLOWED TO MUTATE 'tokens' (e.g. pop())!!! YOU MUST USE 'i' TO GET THE CURRENT TOKEN OUT OF 'tokens' """ if tokens == []: raise TypeError("Error: Empty List.") elif tokens[int(i)] == '*': tokens.remove(int(i)) return mkMultiplyNode(parse(tokens), parse(tokens)) elif tokens[int(i)] == '//': tokens.remove(int(i)) return mkDivideNode(parse(tokens), parse(tokens)) elif tokens[int(i)] == '+': tokens.remove(int(i)) return mkAddNode(parse(tokens), parse(tokens)) elif tokens[int(i)] == '-': tokens.remove(int(i)) return mkSubtractNode(parse(tokens), parse(tokens)) elif tokens[int(i)].isdigit(): return mkLiteralNode(tokens.remove(int(i))) elif not tokens[int(i)].isdigit(): return mkVariableNode(tokens.remove(int(i))) else: raise TypeError("Error: Invalid Input") ############################################################################## # main ############################################################################## def main(): """main: None -&gt; None The main program prompts for the symbol table file, and a prefix expression. It produces the infix expression, and the integer result of evaluating the expression""" print("Hello Herp, welcome to Derp v1.0 :)") inFile = input("Herp, enter symbol table file: ") symTbl = {} for line in open(inFile): i = line.split() symTbl[i[0]] = int(i[1]) print("Derping the symbol table (variable name =&gt; integer value)...") for variable in sorted(symTbl): print(variable + " =&gt; " + str(symTbl[variable])) # STUDENT: CONSTRUCT AND DISPLAY THE SYMBOL TABLE HERE print("Herp, enter prefix expressions, e.g.: + 10 20 (RETURN to quit)...") # input loop prompts for prefix expressions and produces infix version # along with its evaluation while True: prefixExp = input("derp&gt; ") if prefixExp == "": break # STUDENT: GENERATE A LIST OF TOKENS FROM THE PREFIX EXPRESSION prefixLst = prefixExp.split() # STUDENT: CALL parse WITH THE LIST OF TOKENS AND SAVE THE ROOT OF # THE PARSE TREE. tokens = [] parseLst = parse(prefixLst, tokens) # STUDENT: GENERATE THE INFIX EXPRESSION BY CALLING infix AND SAVING # THE STRING infixLst = infix(parseLst) print("Derping the infix expression:") # STUDENT: EVALUTE THE PARSE TREE BY CALLING evaluate AND SAVING THE # INTEGER RESULT print("Derping the evaluation:") print("Goodbye Herp :(") if __name__ == "__main__": main() </code></pre> <p>The error I received is: </p> <pre><code> File "derpNew.py", line 31, in parse if tokens[int(i)] == '*': TypeError: int() argument must be a string or a number, not 'list' </code></pre> <p>If I remove the int() from the variable <code>i</code>, then I would get this error: <code>TypeError: list indices must be integers, not list</code></p> <p>Am I suppose to convert the list to tuple? Any help would be great. Thank you.</p> <p>If you guys are curious how I am calling the parse. I put this under main function.</p> <pre><code> tokens = [] parseLst = parse(tokens, i) </code></pre> <p><strong>EDIT:</strong> The loop:</p> <pre><code>while True: prefixExp = input("derp&gt; ") if prefixExp == "": break prefixLst = prefixExp.split() tokens = [] parseLst = parse(tokens, i) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload