Python Forum
Very basic programming help needed: pig latin program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Very basic programming help needed: pig latin program
#1
VOWELS = ('a','e','i','o','u','A','E','I','O','U')
word = input("Welcome to the Pig Latin translator, simply enter a word and the translator will return the word in Pig Latin. Insert your word below. \n> ")

def pigLatin(word):
    if word[0] in VOWELS:
        print (word + 'ay')
    else:
        print (word[1:] + 'ay')
pigLatin(word)

playAgain = input("Would you like to play again? \n> ")

def secondGame(playAgain):
    if playAgain == ('yes'):
        second = input("Enter your next word below \n> ")
        pigLatin(second)
    elif playAgain == ('no'):
        print ('Thanks for playing!')
    else:
        print ('Error: Please respond with a yes or no answer.')

secondGame(playAgain)
________________________________________________________________________________________________

Above is my code. It runs fine. I basically just had it run through a second time using the secondGame function I made. This still ends the game after only two words have been entered (if you choose the yes option to play again). 

My question is - What would be a better way to loop this program? I want it to continue allowing me to enter words into the program until I tell the program "no" - I do not want to play again. 

Please help!

Thanks,
bstocks
Reply
#2
You want a while loop. Something like:

play_again = 'yes'
while play_again == 'yes':
    # pig latin code
    play_again = input('Would you like to play again (yes or no)? ')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
def translate(text):
    return ' '.join('{}{}{}'.format(word, word[0], 'say')[1:] for word in text.split())

while 1:
    word = input('Enter some words: ')
    if word == 'quit':
        break
    print(translate(word))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 551 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  non-latin characters in console from clipboard Johanson 3 687 Oct-26-2023, 10:10 PM
Last Post: deanhystad
  "If Len(Word) == 0" Code Block in Pig Latin Program new_coder_231013 3 2,047 Jan-02-2022, 06:03 PM
Last Post: deanhystad
  trying to understand a string literal in a basic server program CompleteNewb 4 2,101 Nov-14-2021, 05:33 AM
Last Post: deanhystad
  PLEASE HELP! Basic Python Program joeroffey 5 2,418 Apr-10-2021, 02:59 PM
Last Post: jefsummers
  Pressing non-latin characters? Murlog 0 1,517 Jul-25-2020, 03:10 PM
Last Post: Murlog
  Absolutely new to python - basic advise needed mariolucas75 2 2,045 Jun-12-2020, 08:36 PM
Last Post: Yoriz
  I want to filter out words with one letter in a string (pig latin translator) po0te 1 2,096 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Basic programming problem darek88 1 1,982 Sep-30-2019, 01:13 PM
Last Post: ichabod801
  Python basic program webshakeup 4 2,769 Sep-19-2019, 07:14 AM
Last Post: Maheshsharma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020