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

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/python-beginner-2W5v7/1/4?curriculum_id=4f89dab3d788890003000096

<In what way does your code behave incorrectly? Include ALL error messages.>
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.

<What do you expect to happen instead?>

[spoiler] ```python 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’

[/spoiler]
<do not remove the three backticks above>

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.

1 Like

All right, thanks for the answer!

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"
1 Like

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.