1

I have this piece of script which works in python 2.7 but it doesnt on 3.6

with open("points.txt") as myfile:
    print '{0:10} {1:5}'.format("Exercices","Points")
    for line in myfile:
        entry = line.strip().split(",")
        sum = sum + int(entry[1])
        print "{0:^10} {1:^5}".format(entry[0], entry[1])
    print "{0:^10} {1:^5}".format("Results",sum)

I get a

 File "points.py", line 18
    print '{0:10} {1:5}'.format("Exercices","Points")
                       ^
SyntaxError: invalid syntax

Is it because I need to use f string instead?

Thanks.

Update on Solution

I missed the parenthesis..

Correct one is:

with open("points.txt") as myfile:
    print ('{0:10} {1:5}'.format("Exercices","Points"))
    for line in myfile:
        entry = line.strip().split(",")
        sum = sum + int(entry[1])
        print("{0:^10} {1:^5}".format(entry[0], entry[1]))
    print("{0:^10} {1:^5}".format("Results",sum))
Olive.b
  • 93
  • 4

0 Answers0