Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Basic Python Programming Help?
I'm watching video on computer science from MIT. The professor did a simple program in python.
It showed on the screen :
x = 15
if (x/2)*2 == x:
print 'Even'
else: print 'odd'
print, else and if were orange, and Even and odd were green.
in my case it showed print in purple,
x = 15
if (x/2)*2 == x:
print 'Even'
but when I pressed enter it said : SyntaxError: invalid syntax at the last apostrophe in 'Even'
why is this? I have Python v3.1.3
it did this with other sample programs I found on the internet.
how can I fix this?
3 Answers
- omLv 61 decade agoFavorite Answer
In Python 2, which is what the professor was using, 'print' is a keyword and it is treated specially.
In Python 3, which is what you're using, 'print' is no longer a keyword. It's just the name of a built-in function, and like any function its arguments must be given inside parentheses. So where you currently have this:
print 'Even'
you must use this instead:
print ( 'Even' )
This is one of a handful of incompatibilities between Python 2 and Python 3. The changes are summarised at http://docs.python.org/py3k/whatsnew/3.0.html The 'print' change is the first on the list.
At the moment, most of the examples and tutorials you'll find are written in Python 2, so if you're using Python 3 you should take a few moments to read that list. That way you'll have a chance of recognising what's going on when you run into one of the things that behaves differently.
- 1 decade ago
Make sure you have your code properly indented, most python errors aris from indentation, make sure its like this:
x = 15
if (x/2)*2 == x:
print 'Even (thats tabbed across)
I have tried that on my version of python and it works when indented.
Good luck programming in python, its a great langague,
Matt
- Anonymous5 years ago
The second is done as such: declare a sum variable for each value if it's zero write sum variable and exit loop if it's not add it to the sum variable