What does the "eval()" function do in Python?

Question:

Please consider this code:

import random
name = input("Welcome to this Arithmetic quiz,please enter your name:")

number1 = random.randint(20,50)
number2 = random.randint(1,20)
oper = random.choice('+-*')

result = eval(str(number1)+oper+str(number2))
answer = (int(input('What is:'+str(number1)+oper+str(number2)+'=')) == result)

if answer == True:
    print('Correct!')
else:
    print('Incorrect!')

What is the difference between the answer and result variables and what is the point of eval? What’s its purpose?

Asked By: PythonNooby

||

Answers:

In your code, the answer variable is what the user enters in response to your request for input. It’s their answer to your question. The result variable is the correct answer to your randomly generated question. What you want to do is check whether their answer is the same as the expected result (which is why I called that variable correct_answer in my answer to your previous question).

The reason for using eval is that it evaluates an expression which is passed as a string. So for example if you pass “2*3” to eval it will return 6.

The documentation for eval is here which is always a good place to start.

Breaking down the line which is giving you trouble:

result = eval(str(number1)+oper+str(number2))

Breaks down for an example where number1 is 4 and number2 is 7:

result = eval("4*7")

The “4*7” is then converted by eval from the string “4*7” to the source code 4*7 which is then calculated out to be 28.

I think the difficulty you’re having is seeing that there is a difference between “4*7” which is a string, and 4*7 which is a code statement. What eval does is convert strings to statements which can then be executed as code.

Answered By: Jamie Bull

Documentation says:

eval(expression, globals=None, locals=None)

The expression argument is parsed and evaluated as a Python
expression

In short, the string argument passed to eval() is executed as normal python instructions. For example:

hello_printer = 'print("hello")'
eval(hello_printer)

prints out hello onto screen, as would be done when executing print("hello") instruction. Python interpreter simply tries and executes the string argument passed to eval function.

The argument to your eval function is str(number1)+oper+str(number2), which is broken down to:

  • convert number1 to string form
  • concat the operator to number1 which is string form
  • convert the number2 to string form and concat it to the previous

e.g. if number1 is 10, oper is +, and number2 is 20, then this makes the string '10+20', which is passed to eval function and evaluated as 10+20. Then the evaluated value is stored in the variable result. Finally result is compared to the answer provided by the user. If they match, True is stored in answer, otherwise False, which is then checked to give the output of ‘correct’ or ‘incorrect’

Answered By: Sнаđошƒаӽ
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.