#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Question Bug in Pig Latin to English translator please help me.


    Hi,

    I'm creating a Pig Latin to English Translator on python but i can't get it to evaluate whether a variable is a vowel or not this will hopefully make more sense after you read the code.
    Code:
    pyg = 'ay'
    
    print "Welcome to the English to Pig Latin translator!"
    original = raw_input("Please enter a word to be translated into Pig Latin. ")
    
    lenght = len(original)
    word = original.lower()
    first = word [0]
    
    if lenght >= 2 and lenght <= 25 and original.isalpha():
        if first == "a" or "e" or "i" or "o" or "u":
            print "vowel"
        else:
            print "consonant"
    else:
        print "you did not enter a valid word please try again"
    this is not the whole program because when I run this so far it doesn't work.

    the line python throws up as wrong is...
    Code:
    if first == "a" or "e" or "i" or "o" or "u":
    please what am i doing wrong what is wrong with the code, is it the "", or is it using or, please help.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,708
    Rep Power
    480

    Mistake extremely common


    Just because it reads like what you mean...

    This conditional expression evaluates to True or to 'e' . Either of which is True to the if statement.

    first == "a" or "e" or "i" or "o" or "u":


    You can write either

    first in 'aeiou'

    or you can write the expression as

    (
    (first=='a') or
    (first=='e') or
    (first=='i') or
    (first=='o') or
    (first=='u')
    )


    I surely hope you can tell the difference. We're not looking at expression format here.


    Python, however, does support "do what I mean" with comparisons. Where you have

    lenght >= 2 and lenght <= 25

    You can actually use*

    2 <= lenght <= 25


    Which wouldn't work "as it reads" in any other programming language I know. (I don't know ruby. Wouldn't surprise me if the feature were copied into ruby. Actually I expect it is.)

    *I hope I misspelled length as you did.
    Last edited by b49P23TIvg; September 6th, 2013 at 04:30 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0
    Thanks for your help sorry about the spelling. i will try to tell you if the code works, i haven't had time to run the code because of other things

    Comments on this post

    • b49P23TIvg agrees : Thank you. It will be quite useful to know if my code works.

IMN logo majestic logo threadwatch logo seochat tools logo