Just wanted to make sure this was the best way to go about it


#1

eng = raw_input("What'd ya say?")
if len(eng) > 0 and eng.isalpha():
    eng = eng.lower()
    print "Ahh... You mean " + eng[1:len(eng)] + eng[0] + "ay"
else:
    print "Louder kid! These old ears of mine don't work as well as they used to."

#2

Hi @javablaster76400,

What do you mean by - "Just wanted to make sure this was the best way to go about it"? Do you need help with this working code?


#3

@gaurangtandon ,

I completed the project tutorial, but thought that there had to be a better way to implement the program. The code above is my attempt to simplify the project.

I was hoping that someone with some more expertise could tell me if this is as simple as the code. If it can be written more easily I would greatly appreciate a bit of guidance.


#4

I must say that your code is very concise :slightly_smiling: Although, I will not make this more concise, but write in a manner that supports coding in the long run:

def pyg_latin(word):
    return word[1:] + word[0] + "ay" # no need for len(word)

def process():
    eng = raw_input("What'd ya say? Enter nay to quit")

    if eng == "nay":
        print "Good bye kid!"
        return False
    
    if len(eng) > 0 and eng.isalpha():
        eng = eng.lower()
        print "Ahh... You mean " + pyg_latin(eng)
    else:
        print "Louder kid! These old ears of mine don't work as well as they used to."
    
    process()    

process()

Run it on repl

What I have essentially done is broken up the logic into functions. In this way, we get the added advantage of reusability. So, at any point of time later in the program, if I need to call pyg_latin, I can freely do so. Similarly, as you must have seen, I am also calling process at the last statement of process itself so that user can try multiple inputs without having to re-run the code.

Hope it helps! :smiley: