In the book that I am reading on Python, it keeps using the code eval(input(\'blah\'))
I read the documentation, and I understand it, but I still do no
I'm late to answer this question but, no one seems to give clear answer to the question.
If an user enters a numeric value, input()
will return a string.
>>> input('Enter a number: ')
Enter a number: 3
>>> '3'
>>> input('Enter a number: ')
Enter a number: 1+1
'1+1'
So, eval()
will evaluate returned value (or expression) which is a string and return integer/float.
>>> eval(input('Enter a number: '))
Enter a number: 1+1
2
>>>
>>> eval(input('Enter a number: '))
Enter a number: 3.14
3.14
Of cource this is a bad practice. int()
or float()
should be used instead of eval()
in this case.
>>> float(input('Enter a number: '))
Enter a number: 3.14
3.14