Some Python Help

Discussion in 'Mac Programming' started by *Y*, May 7, 2006.

  1. *Y*
    macrumors regular

    Joined:
    Oct 19, 2004
    Location:
    VA
    #1
    Hey guys,

    I am pretty new on the programming scene. I have a question for you Python gurus. Why wont this:

    prefixes = "JKLMNOPQ"
    suffix = "ack"
    for letter in prefixes:
    if letter=="O"or"Q":
    print letter + "u"+suffix
    else:
    print letter + suffix

    give me this:

    Jack
    Kack
    Lack
    Mack
    Nack
    Ouack
    Pack
    Quack

    Thanks.:D
     
  2. macrumors 6502

    Joined:
    May 12, 2003
    #2
    You can't combine the test for 'O' and 'Q' that way. You need to do it like this:

    Code:
    prefixes = "JKLMNOPQ"
    suffix = "ack"
    for letter in prefixes:
      if letter == "O" or letter == "Q":
        print letter + "u"+suffix
      else:
        print letter + suffix
    
    Remember, when you are evaluating an expression, both sides of your "or" need to evaluate to a "true" or "false". "Q" isn't a logical value to put there.
     
  3. *Y*
    thread starter macrumors regular

    Joined:
    Oct 19, 2004
    Location:
    VA

Share This Page