jaredec18

Untitled

Sep 19th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. Code:
  2.  
  3. #!/usr/bin/python
  4.  
  5. def main():
  6. # declaraing the variable and initializing to their default values
  7. word=""
  8. vowels = 'aeiou'
  9. v_index = -1
  10. flag = 1
  11.  
  12. # running an infinite loop
  13. while(flag):
  14. # Taking word as input from user
  15. word = raw_input("Enter a word: ")
  16. # computing length of the word and converting word to lower case
  17. w_len = len(word)
  18. word = word.lower()
  19. # if the word entered is 'quit' it will quit out from the loop and program terminates
  20. if word == 'quit':
  21. break
  22. # iterating through each letter in word to find the first position of vowel
  23. for i, ch in enumerate(word):
  24. if ch in vowels:
  25. v_index = i
  26. break
  27. # if v_index != -1 that means given word has a vowel
  28. if v_index != -1:
  29. # if v_index is 0 that means vowel is at first position in the word
  30. if v_index == 0:
  31. print word + 'way'
  32. # if v_index is > 0 that means vowel is not at first position but somewhere in the middle or last
  33. elif v_index > 0:
  34. print word[v_index:w_len] + word[0:v_index] + 'ay'
  35.  
  36.  
  37. if __name__=='__main__':
  38. main()
  39.  
  40.  
  41. Execution and output:
  42. 186590cb0725:Python bonkv$ python pig_latin.py
  43. Enter a word: dog
  44. ogday
  45. Enter a word: scratch
  46. atchscray
  47. Enter a word: is
  48. isway
  49. Enter a word: apple
  50. appleway
  51. Enter a word: Hello
  52. ellohay
  53. Enter a word: a
  54. away
  55. Enter a word: quit
  56. 186590cb0725:Python bonkv$ python pig_latin.py
  57. Enter a word: bat
  58. atbay
  59. Enter a word: QUIT
  60. 186590cb0725:Python bonkv$ python pig_latin.py
  61. Enter a word: ball
  62. allbay
  63. Enter a word: Quit
Advertisement
Add Comment
Please, Sign In to add comment