Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Code:
- #!/usr/bin/python
- def main():
- # declaraing the variable and initializing to their default values
- word=""
- vowels = 'aeiou'
- v_index = -1
- flag = 1
- # running an infinite loop
- while(flag):
- # Taking word as input from user
- word = raw_input("Enter a word: ")
- # computing length of the word and converting word to lower case
- w_len = len(word)
- word = word.lower()
- # if the word entered is 'quit' it will quit out from the loop and program terminates
- if word == 'quit':
- break
- # iterating through each letter in word to find the first position of vowel
- for i, ch in enumerate(word):
- if ch in vowels:
- v_index = i
- break
- # if v_index != -1 that means given word has a vowel
- if v_index != -1:
- # if v_index is 0 that means vowel is at first position in the word
- if v_index == 0:
- print word + 'way'
- # if v_index is > 0 that means vowel is not at first position but somewhere in the middle or last
- elif v_index > 0:
- print word[v_index:w_len] + word[0:v_index] + 'ay'
- if __name__=='__main__':
- main()
- Execution and output:
- 186590cb0725:Python bonkv$ python pig_latin.py
- Enter a word: dog
- ogday
- Enter a word: scratch
- atchscray
- Enter a word: is
- isway
- Enter a word: apple
- appleway
- Enter a word: Hello
- ellohay
- Enter a word: a
- away
- Enter a word: quit
- 186590cb0725:Python bonkv$ python pig_latin.py
- Enter a word: bat
- atbay
- Enter a word: QUIT
- 186590cb0725:Python bonkv$ python pig_latin.py
- Enter a word: ball
- allbay
- Enter a word: Quit
Advertisement
Add Comment
Please, Sign In to add comment