-1

I have googled the whole internets and can't find the reason why I get this error when using range() function:

>>> for x in range(5):
     print "Hello World!"

SyntaxError: invalid syntax

I expect 5 Hello Worlds there.

It's ok on Python 2.7, but on Python 3.3.3 (64bits, Windows 8.1) I get this error. Could anybody advice how can I make loops in Python 3.3.3? Is it bug or something has changed a lot since 2.7 regarding "For"?

Thanks. :/

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
RIXLV
  • 1
  • 1
  • `print` is a function in Python3 – Ashwini Chaudhary Jan 26 '14 at 19:36
  • 4
    The whole internets? You need a new browser, it's suppressing results for some reason. :^) The first google result for "python 3.3 syntax error" explains the problem. – DSM Jan 26 '14 at 19:37
  • Yes, but I couldn't imagine that Print() is the guilty one. :) Was too confident that problem was in range. My fault! – RIXLV Jan 26 '14 at 19:49

3 Answers3

2

print is a function in Python 3, you need to put parentheses:

for x in range(5): 
    print("Hello World!")

From the official website:

The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105).

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
0

In Python 3.x, print is a function, so you must call it with parentheses ():

print("Hello World!")
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

In python 3 and above print is a function so try writing

for x in range(5): print("Hello World!")
Vipul
  • 4,038
  • 9
  • 32
  • 56