learning to program with Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ricardo

    learning to program with Python

    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 "<interacti ve input>", line 5
    if corPelo != None
    ^
    SyntaxError: invalid syntax

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

    Ricardo


  • stewart

    #2
    Re: learning to program with Python

    Ricardo wrote:
    [color=blue]
    > The code in wich i'm
    > having problems is as follows:
    >[/color]
    ....[color=blue]
    > if corPelo != None
    > self.corPelo = corPelo[/color]
    ....

    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

    Comment

    • Dennis Lee Bieber

      #3
      Re: learning to program with Python

      On Mon, 23 Feb 2004 06:15:32 GMT, stewart
      <stewart@midtoa d.homelinux.org > declaimed the following in
      comp.lang.pytho n:
      [color=blue]
      > 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[/color]

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

      --[color=blue]
      > =============== =============== =============== =============== == <
      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
      > wulfraed@dm.net | Bestiaria Support Staff <
      > =============== =============== =============== =============== == <
      > Home Page: <http://www.dm.net/~wulfraed/> <
      > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

      Comment

      • Nicolas Fleury

        #4
        Re: learning to program with Python

        stewart wrote:[color=blue]
        > 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[/color]

        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

        Comment

        Working...