Mungo Roths
Mungo Roths

Reputation: 1

TypeError: locals must be a mapping

In Python 3.4.3 I have this code.

operator = random.choice('-+')  
    numTheF = random.choice(numsUseF)  
    numTheS = random.choice(numsUseS)   
    print ('What is', int(numTheF), operator, int(numTheS))  
    ansReal = eval(int(numTheF), operator, int(numTheS))  
    ansUser = input ('?')  
    if ansUser == ansReal:    
        score += 1  
        question += 1   

All the variables are properly set up but I can't get the ansReal to output the actual answer.

Here is the error message.

Traceback (most recent call last):
  File "C:\Users\Marko\Documents\Programming Princ\Task One.py", line 34, in       <module>
ansReal = eval(int(numTheF), operator, int(numTheS))
TypeError: locals must be a mapping

Upvotes: 0

Views: 3742

Answers (1)

Blacklight Shining
Blacklight Shining

Reputation: 1548

eval() takes three arguments, the latter two of which are optional. The first is the expression (as a string) to evaluate; the latter two, if present, must be mappings that will be used for the global and local namespaces in which to evaluate the expression. See the documentation on eval() for more information.

As Kevin, I do not condone the use of eval(). You're already using int() to parse strings into integers; that's a good start—consider also simply adding or subtracting those integers to get the real answer.

Upvotes: 1

Related Questions