0

I wrote this code, which I found in the book 'Python for dummies'

countdown = 10
while countdown:
    print countdown,
    countdown -= 1
print "Blastoff!"

It should print 10 9 8 7 6 5 4 3 2 1 Blastoff!

When I run the program, I get an error 'Missing parentheses in call to print'

On youtube I found a code similar to this, which counts from 0 to 10000000. This one works just fine.

def count(x):
while (x <= 10000000):
    print (x)
    x+=1
count(0)
print ("I hate my job. I quit!")

How comes they look so different? What kind of basic knowledge do I need to understand this? Is this a question of different python versions? Is 'Python for dummies' a bad book?

3 Answers3

4

This is a Python 2 vs 3 gotcha. print was a keyword in 2, now it's a function in 3. Evidently that code was written for 2, but you're using 3.

Just add parentheses and treat print as you would any other function.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
2

For Python 3.0 and after () are required for `print̀ .

Change to print ("Blastoff!") you won't have any error

Nuageux
  • 1,668
  • 1
  • 17
  • 29
1

The book you are reading is written for python version 2, where if and while statements do not require parenthesis. However, you seem to be using python version 3, which requires you to put paranthesis in if and while statements. You can solve this issue by adding paranthesis to the code in the right places, or download python version 2 (2.7 is the most common version) so you can use the code without modifying it.

Meccano
  • 537
  • 4
  • 8