Variables in an IF statement


#1

why is PYG placed outside the IF statement, but the other variable are withing the IF statement, and how do you know when to do each of these options?

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)]
else:
print ‘empty’


#2

its logic design for the program. This allows us to easily change pyg in case we want something different. The rest of the code is really something we want to do if the user enters a valid word, which is why its placed inside the if clause

pyg doesn’t depend on valid word or not. As such, its not logic to place inside the if clause

This is a design choice, picking the right design for your program is important, and something you will learn by practice


#3

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