0

Here is the code:

def print_variables(vars):
    for i in range(len(vars)):
        print(f"{i + 1}:  {vars[i]}")


print_variables(["Red", "Green", "Blue", "Orange", "Yellow"])

On the 3rd line it is giving the error "Invalid Syntax" and I don't know why. I am new to python.

bharatk
  • 4,202
  • 5
  • 16
  • 30

2 Answers2

0

I am guessing you are on an older version of Python when f'strings weren't introduced. So please upgrade to python 3.6 or newer. Otherwise, do this: For python 3.x:

print("{0}:  {1}".format((i+1),(vars[i])))

For Python 2.x:

print "%s:  %s" % ((i+1),(vars[i]))
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
0

It is working fine in python 3.7.4. It was introduced from 3.6. check this f-strings giving SyntaxError? If python version you are using is below 3.6, it may be the reason that it is giving an error.