Eval () works on macOS but not Windows

I am working on Python programming (for Python 3) by John Zell and until recently did all my work on a MacBook Pro running 10.7. I am working in Eclipse Juno on both platforms. I decided to transfer all my projects to my Windows 7 machine and imported them into Eclipse Juno. I noticed that every app eval(input())

was broken, but they all worked on a Macbook. The same goes for any code I entered from the book. Why does this work on one platform but not another? Here's some sample code that works on MacOS but not Windows:

def main():
    sum = 0.0
    count = 0
    xStr = input("Enter a number (<Enter> to quit) >> ")
    while xStr != "":
        x = eval(xStr)
        sum = sum + x
        count = count + 1
        xStr = input("Enter a number (<Enter> to quit) >> ")
    print("\nThe average of the numbers is", sum / count)

main()

      

This works fine on Mac, but spits out this error on Windows:

Enter a number (<Enter> to quit) >> 5

Traceback (most recent call last):
  File "C:\Users\Nostromo\workspace\Chapter 11\average4.py", line 18, in <module>
    main()
  File "C:\Users\Nostromo\workspace\Chapter 11\average4.py", line 12, in main
    x = eval(xStr)
  File "<string>", line 1
    5    
    ^
SyntaxError: unexpected EOF while parsing

      

+3


source to share


2 answers


What if you change input () to raw_input ()?



+2


source


eval(input())

is a stupid way to get what you want anyway, and the author of the book shouldn't suggest that you use it. Change it to int(input())

and you will be much happier.



+1


source







All Articles