Is it ok to code like this?


#1



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

Hi all! I was wondering what difference it would make if I used the following code instead of the code I generated using the step-by-step approach in the lesson?

The code I wrote I did not use the string 'pyg' and 'original', and I did the lower case conversion in line 1.

Is it ok to use the code I wrote? What are the advantages or disadvantages between these two different codes?

Thank you very much for your help!


#The code I try to use
word = raw_input('Enter a word:').lower()
if len(word) > 0 and original.isalpha():
    print word
    print word[1:len(word)]+word[0]+"pyg"
else:
    print 'empty'

#Step-by-step approach
pyg = 'ay'
original = raw_input('Enter a word:')
word=original.lower() #convert to lower case
if len(original) > 0 and original.isalpha():
    print original
else:
    print 'empty'
first=word[0]
new_word=word+first+pyg
new_word=new_word[1:len(new_word)]
print new_word


#2

The only difference is this one,

is codecademy's version of a pyglatin translator.

I highly encourage what you have done because it shows you are already thinking like a programmer. The only problem is your code is DRY but it does not print the desired result.

For your code to work you only need to change one line,

to,

print word[1:len(word)]+word[0]+"ay"

And also you don't need this line anymore,


#3

Can you elaborate on this?


#4

DRY stands for don't repeat yourself. For example in the codecademy lesson you have:

original = raw_input('Enter a word:')
word=original.lower()

while you could do this in one line (like you did), so you don't repeat yourself, is meant as a positive thing (your program is more efficient), just make sure it stays readable :slight_smile: (which you did)


#5

The only problem is it didn't print the desired result which I elaborated as well.


#6

i know, but it seems OP didn't seem to know DRY (despite applying these principals), so i thought it would be more valuable for my answer to talk about DRY, you already mentioned these mistakes.


#7

Sorry I thought I sent it to @supernovaderek :sweat_smile:


#8

I got it! Thank you both @bandit @stetim94!!


#9