ok so the code is
pyg = 'ay'
print "To translate type A SINGLE word or name!"
original = raw_input("Type word Here:")
while len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word = word[1:] + first + pyg
print "Translating 1 moment..."
print "Translated view below!"
print new_word
print "Made by: Tobias Balentine"
raw_input ('pause')
if raw_input ("Do you want to start over?").lower().[0] != 'y': break
so my question is how do i restart to the start of the code without exiting the program when i put (if raw_input ("Do you want to start over?").lower().[0] != 'y': break) it just shows the translation again but i want it to go to the start of the code so you can type a different word to translate how would i go about doing this?
P.S. i am new to python
Copy this statement:
original = raw_input("Type word Here:")
inside your while statement. Your final code should be like this:
pyg = 'ay'
print "To translate type A SINGLE word or name!"
original = raw_input("Type word Here:")
while len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word = word[1:] + first + pyg
print "Translating 1 moment..."
print "Translated view below!"
print new_word
print "Made by: Tobias Balentine"
raw_input ('pause')
if raw_input ("Do you want to start over?").lower().[0] != 'y':
break
original = raw_input("Type word Here:")
As others have indicated, if you want to continually prompt the user for new words, you need to have that prompt be inside of the loop. You could do something like this, where you prompt the user in the loop, and then, if the input is valid, translate the word. If it's not, tell the user so. Then, in either case, you ask the user if she wants to start over, and break the loop if she doesn't say yes (or more accurately, if she types anything that doesn't start with a "Y" or "y").
pyg = 'ay'
print "To translate type A SINGLE word or name!"
while True:
original = raw_input("Type word Here:")
# Validate the input here. If it's valid,
# do the translation.
if original and original.isalpha():
word = original.lower()
first = word[0]
new_word = word[1:] + first + pyg
print "Translating 1 moment..."
print "Translated view below!"
print new_word
else:
# Tell the user if his input is invalid.
print "Your input was stupid. No translation 4 u."
# Ask the user if he wants to start over
if raw_input ("Do you want to start over?").lower()[0] != 'y':
break
# Give em something to remember you by
print "Made by: Tobias Balentine"
Related
Hi i made a pyglatin code:
pyg = 'ay'
original = 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)]
print(new_word)
else:
print("empty")
and it works perfectly but i need s.th else.
for example if user entered
"rm !"
then the output should be:
"mray !"
i dont punctuation marks like ! , . # to change.
can anyone help?
So this code has worked correctly without the try clause, but I need it to work in this manner because I want to continue prompting the user for input and give an option to exit. Ive searched and cant find a similar questions asked. Can someone please take a look and see what Im missing, thanks.
vowels = 'aeiou'
def converter(word):
while True:
try:
first = word[0]
if first in vowels:
word = word + 'yay'
return word
else:
while word[0] not in vowels:
word = word[1:] + word[0]
word = word + 'ay'
return word
split = word.split()
translated = [converter(word) for word in split]
translated = " ".join(translated)
print(translated)
except ValueError:
if word.lower() == "exit":
word = word.lower()
print("Adios")
else:
print("Try again.\n")
converter(input('Words to translate into piglatin: '))
The problem with the code is that it enters an infinite while loop in the converter() function.
You should move the while loop outside the function:
while True:
converter(input('Words to translate into piglatin: '))
Then, you can process the input to check if the user wants to quit. If the user chooses not to quit, then you should pass the input string to the converter() function. You already have most of the components needed looking at your CodeHS link without the try/except block.
For an assignment, I need code that asks the user for a word and a letter. Then, it edits the word to not include the specific letter. It needs in include a "for i in range" statement. The code before works but doesn't use a for loop and uses a python command.
word1 = raw_input ("Give me a word! ")
letter1 = raw_input ("Give me a letter! ")
modify = word1.replace(letter1,"")
check = word1.find(letter1)
if check == -1:
print "There is no letters to replace in", word1
check = 0
if check >= 1:
print modify
How about:
word = raw_input('Give me a word! ')
letter = raw_input('Give me a letter! ')
cleaned = ''
for i in range(len(word)):
if word[i] != letter:
cleaned += word[i]
if cleaned:
print cleaned
else:
print 'There is no letters to replace in', word
You can iterate through a string letter by letter like you would a list or dict
word='someword'
for letter in word:
print(letter)
Using the Codecademy pyglatin.py translator as an example, I am trying to expand the translator to include multiple words at a time.
So far, it reads the first word and translates it, and I would like to continue onto the next word, and then the next, until no more words exist. I would then like to print the entire original translated input.
def piglatin():
pig = 'ay'
original = raw_input('Enter a phrase:').split(' ')
if len(original[0]) > 0 and original[0].isalpha():
word = original[0].lower()
first = word[0]
if first == "a" or first == "e" or first == "i" or first == "o" or first =="u":
new_word = word + pig
print new_word
else:
new_word = word[1:] + word[0:1] + pig
print new_word
again = raw_input('Translate again? Y/N')
print again
if len(again) > 0 and again.isalpha():
second_word = again.lower()
if second_word == "y":
return piglatin()
else:
print "Okay Dokey!"
else:
print 'Letters only please!'
return piglatin()
Thanks!
You want a for loop. A good starting point would be:
for word in original:
Here is my code for the pig latin translator. It works both on Code academy and in linux terminal.
pyg = 'ay'
new_word = pyg
original = raw_input('Enter a word: ')
if len(original) > 0 and original.isalpha():
original.lower()
word = original
first = original[0]
if first == 'a' or first =='e' or first == 'i' or first =='o' or first == 'u':
print 'vowel'
elif first != 'a' or first !='e' or first !='o' or first !='i' or first !='u':
print word.lower()[1:] + first +new_word
else:
print 'empty'
Code academy gives following result;
Oops, try again! Your word started with a consonant, but “ay' was printed instead of “ogday”. Make sure the correct value #is stored in “new_word”.
"ay" is not printed but "ogday' is printed.
Does anyone know how to fix this? I cannot continue with Codeacademy as without solving this.
You can do something like this for example. You are in the right track just use what you have learned in the Code academy up to this task.
consonants = "bcdfghjklmnpqrstvxz"
original = raw_input('Enter a word: ')
if len(original) > 0 and original.isalpha():
if original.lower()[0] in 'aeiou':
print original.lower() + 'ay'
else:
keep_first_consonants = ''
count = 0
for letter in original.lower():
if letter in consonants:
keep_first_consonants = keep_first_consonants + letter
count += 1
else:
break
total_characters = len(original.lower())
print original.lower()[count:total_characters] + keep_first_consonants + 'ay'
else:
print 'Accept only letters'
The codeacademy lesson checker seems to check the variable new_word when you hit run
So you just need to use new_word for both your print varibles
This code works:
pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
if first == "a" or first == "e" or first == "i" or first == "o" or first == "u":
new_word = original + pyg
print new_word
else:
newer_word = word[1:]
new_word = newer_word + first + pyg
print new_word