Pyg Latin works, but is it done the right way?


#1

This code works as expected, but since i’m doing it in a loop, am I doing it correctly?
I feel that there is some better, cleaner way to do it?

def getWord():
  word = raw_input("Enter a word in English and hit 'Enter': ").lower()
  return word

def checkWord(word):
  if len(word) > 2 and word.isalpha():
    print(word[1:] + word[0] + "ay")
    return True
  else:
    print("Type a real word AND greater than 2 chars!\n")
    return False

result = False
while result == False:
  word = getWord()
  result = checkWord(word)

Thanks for your feedback!


#2

What you’ve done here does make sense, but the PygLatin program is small enough/quick enough to not really need two different functions. It might be seen as wasting time to have it call word and checkWord separately instead of allowing the program to quickly execute based off of user input.

A way to do that would be:

 pyg = "ay"

print ("Welcome to the Python PigLatin interpreter.")

firstResponse = raw_input ("Please enter your first word")

if len(firstResponse) > 0 and firstResponse.isalpha():
    word = firstResponse.lower()
    first = word[0]
    new_word = word + first + pyg
    new_word = new_word[1:len(new_word)]
    print (new_word)

elif firstResponse != firstResponse.isalpha() and len(firstResponse) > 0:
    print ("No numbers, please. This is a translator not a math program.")

else:
    print ("No word entered")

#3

Thanks for the reply. I agree that it’s simple enough and the objective of the exercise really is to implement what was learned so far. My question runs away a little from the exercise wants.

My point in this question is less about the logic, and more about the structure. I.e. i intentionally wanted to keep re-prompting the user until the condition was satisfied. It felt to me that forcing result = False and using that as the conditional for the while loop is a little odd.


#4

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