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


#1



https://www.codecademy.com/courses/python-beginner-2W5v7/1/4?curriculum_id=4f89dab3d788890003000096


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
else:
    print 'empty'


#3

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.


#4

All right, thanks for the answer!


#5

This issue is to pass the lesson you need the following variables:
original
word
first
new_word

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
else:
    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:]
else:
    print  "empty"

#6

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


#7

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