Search code examples
pythonpython-3.xpydeveclipse-juno

eval() works in MacOS but not Windows


I'm working through Python Programming (For Python 3) by John Zelle and up until recently had been doing all my work on a MacBook Pro running 10.7. I'm working in Eclipse Juno on both platforms. I decided to move all my projects to my PC with Windows 7 and imported them into Eclipse Juno. I noticed that every app with an eval(input()) was broken, yet they had all worked on the Macbook. The same is new for any code I enter from the book. Why does this work on one platform but not the other? Here's an example of code that works in 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 the Mac but spits out this error in 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

Solution

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