11. Testing, Testing, is this thing on?


#1

We are asked to place the variables word, first and new_word within the "if" statement, like so -

print "Welcome to PygLatin"

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'

My qusetion is, why do they ask us to place those variables inside the "if" statement ? I have seen that even if those variables are placed outside the "if" statement, the code runs successfully. For example -

print "Welcome to PygLatin"

pyg = 'ay'

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

P.S - Don't know why the indentations aren't showing up in the final post ?


#2

both will work, yet i would use the first method. I don't want my translator to run, if the user enters something invalid


#3

your work works for now , but you haven't defined function and called it , so try to define function and check if both will be valid.