"10. Ending Up" question: is my code effective?



Good day! I just wanted to ask, is my code below is effective? Can it be shorter?
The code works, though – just being curious.
Thanks before.

pyg = 'ay'
original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]
    new_word = word + first + pyg
    new_word = new_word[1:len(new_word)]
    print new_word
    print 'empty'


Your code follows the instructions, which is what it should do. We are not here to improve the lesson code, but to write it and understand the concepts. If you wish to discuss how to improve upon the code, then please take it up in the Corner Bar, not the Q&A. Thanks.


All right, thanks for the answer!


This issue is to pass the lesson you need the following variables:

That doesn't leave us much that we can take out. This is probably as good as you can get it while still passing.

pyg = "ay"

original = raw_input("Enter a word: ")

if original.isalpha():
    word = original.lower()
    first = original [0]
    new_word = (word + first + pyg)[1:]
    print new_word
    print "empty"

As you can see, both the len() parts aren't needed.

One of the shortest you could get it butit will technically fail the lesson due to missing variables:

pyg = "ay"
original = raw_input("Enter a word: ").lower()

if original.isalpha():
    print (original + original[0] + pyg)[1:]
    print  "empty"


Thank you for the answer – the answer I was looking for! I fully understand what you meant.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.