Search code examples
pythonsyntax-errorfindallpython-re

Python2 And Python3


This is for Python 2:

import re

print sum([int(i) for i in re.findall('[0-9]+',open(raw_input('What is the file you want to analyze?\n'),'r').read())])

But why do I get a syntax error with Python 3?

Python3

import re

print sum([int(i) for i in re.findall('[0-9]+',open(input('What is the file you want to analyze?\n')).read())])

Solution

  • That is because in Python3 you should use brackets around the argument for the print function.

    print()

    so your code will work as soon as you write

    print(sum([int(i) for i in re.findall('[0-9]+',open(input('What is the file you want to analyze?\n')).read())]))