learning to program with Python

R

Ricardo

Hi, my name is Ricardo and i'm learning to program in Python with
version 2.3.3, i'm also using the latest version of PythonWin(163) but
i'm trying to do it with a book (Written in Portuguese) that covers the
version 2.1, and of course i'm having a problem. The code in wich i'm
having problems is as follows:

class Gato:
"um felino domesticado"
def __init__(self, sexo, corPelo=None):
self.sexo = sexo
if corPelo != None
self.corPelo = corPelo
def miar(self):
if self.sexo == "m":
return "MIAU"
else:
return "miauuuu"
the book says that it should be done like this, but when i finnish the
5th line(if corPelo != None) and press ENTER, it gives me the error:

Traceback ( File "<interactive input>", line 5
if corPelo != None
^
SyntaxError: invalid syntax

I hope someone can help me?
Thanks in advance anyway.

Ricardo
 
S

stewart

Ricardo said:
The code in wich i'm
having problems is as follows:
....
if corPelo != None
self.corPelo = corPelo
....

Oi Ricardo, tudo bem?

I see at least two problems here. Python requires brackets around the 'if'
comparison, and a colon at the end of the if statement. Try re-writing
your statement as:
if (corPelo != None):
self.corPelo = corPelo

Boa sorte!
S
 
D

Dennis Lee Bieber

I see at least two problems here. Python requires brackets around the 'if'
comparison, and a colon at the end of the if statement. Try re-writing

The colon is needed, yes... but not the parentheses...

--
 
N

Nicolas Fleury

stewart said:
I see at least two problems here. Python requires brackets around the 'if'
comparison, and a colon at the end of the if statement. Try re-writing
your statement as:
if (corPelo != None):
self.corPelo = corPelo

No, Python doesn't require parenthesis or brackets around the
comparison. Any expression can have optional parenthesis.

if corPelo != None:
self.corPelo = corPelo

is enough.

Regards,

Nicolas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top