Translating censored sentences that have been reversed to pyglatin is harder than I thought


#1

So, I decided to be clever and add a bunch of the functions we made in the exercises into one functional program... Unfortunately, I can't seem to get it to work exactly how I want it to...

sentence = raw_input("Type a sentence without using capital letters or punctuation:")
badWord = raw_input("What word would you like to censor from the above sentence?")
error = "An error has occured, please restart the program and try again."

def censor(text, word):
    if word in text:
        return text.replace(word, (len(word) * "*"))
    else:
        return text

def reverse(text):
    anti_text = ""
    for char in text[::-1]:
        anti_text += char
    return anti_text

def pyglatin(text):
    pyg = "ay"
    word = text.lower()
    first = word[0]
    new_word = word + first + pyg
    new_word = new_word[1:]
    if len(text) > 0 and text.isalpha():
        print new_word
    else:
        print error

def askQuestion2(text):
    question_2 = raw_input("How about translating your sentence to pyglatin, yes or no?")
    if question_2 == "yes":
        new_2 = pyglatin(text)
    elif question_2 == "no":
        new_2 = text
    else:
        print error
    print new_2

def askQuestions(text):
    print text
    question_1 = raw_input("Would you like your sentence to be reversed, yes or no?")
    if question_1 == "yes":
        new_1 = reverse(text)
        askQuestion2(new_1)
    elif question_2 == "no": 
        new_1 = text
        askQuestion2(new_1)
    else:
        print error

askQuestions(censor(sentence, badWord))

It works great, until I tell it whether or not I want it to reverse the text. Doesn't matter what I type (yes or no) it always just runs my else statement, meaning it will print my error statement. I don't know what I'm doing wrong...


#2

Reverse seems to work fine! But your function isn't showing the reverse in the console so add print statements (optional).

def askQuestions(text):
    print text
    question_1 = raw_input("Would you like your sentence to be reversed, yes or no?")
    if question_1 == "yes":
        new_1 = reverse(text)
        # print new_1?
        askQuestion2(new_1)
    elif question_2 == "no": 
        new_1 = text
        #print new_1?
        askQuestion2(new_1)
    else:
        print error

Censor conflicts with pyglatin because *** is not alpha. So you'll get error if you censored somthing.
ALSO, spaces don't pass through .isalpha() as well. So if you have spaces between words, the error happens too. I just removed .isalpha().


#3

Hi @aquaphoenix17,

One of the problems is that you define the variable question_2 in the askQuestion2 function, but not in the askQuestions function, and then attempt to test it here, in the askQuestions function ...

    elif question_2 == "no":

If execution gets to that elif block header, it will raise an error.

That is also a problem. It might be best to remove the isalpha method call from the pyglatin function.

Following is an alternative means of organizing the sequence of asking questions and calling functions...

def processQuestion(question, function, text):
    ans = raw_input(question)
    if ans == "yes":
        return function(text)
    elif ans == "no":
        return text
    else:
        print error
        return text

sentence = censor(sentence, badWord)
sentence = processQuestion("Would you like your sentence to be reversed, yes or no?", reverse, sentence)
sentence = processQuestion("How about translating your sentence to pyglatin, yes or no?", pyglatin, sentence)
print sentence

#4

This is what I have now... Tell me what you think:

def newLine():
    print ""

def error():
    print "An error has occured, please restart the program and try again."

def censor(text, word):
    if word in text:
        return text.replace(word, (len(word) * "*"))
    else:
        return text

def reverse(text):
    anti_text = ""
    for char in text[::-1]:
        anti_text += char
    return anti_text

def pyglatin(text):
    pyg = "ay"
    word = text.lower()
    first = word[0]
    new_word = word + first + pyg
    new_word = new_word[1:]
    if len(text) > 0:
        return new_word
    else:
        error()

def askQuestion(text):
    newLine()
    print text
    newLine()
    question_2 = raw_input("How about translating your text to pyglatin, yes or no?")
    if question_2 == "yes":
        new_2 = pyglatin(text)
        newLine()
        print "Translating..."
        newLine()
        print new_2
    elif question_2 == "no":
        new_2 = text
        newLine()
        print "Processing..."
        newLine()
        print new_2
    else:
        error()

def askQuestions(text):
    print "Censoring..."
    newLine()
    print text
    newLine()
    question_1 = raw_input("Would you like your text to be reversed, yes or no?")
    if question_1 == "yes":
        new_1 = reverse(text)
        newLine()
        print "Reversing..."
        askQuestion(new_1)
    elif question_1 == "no":
        new_1 = text
        newLine()
        print "Processing..."
        askQuestion(new_1)
    else:
        error()

def runCode():
    word = raw_input("Type a word:")
    newLine()
    print "Processing..."
    newLine()
    print sentence
    newLine()
    badWord = raw_input("What would you like to censor from the above text?")
    newLine()
    askQuestions(censor(word, badWord))

runCode()

#5

Hi @aquaphoenix17,

Does your most recent post include all of your code? When I copied and ran it, there was an error, as follows ...

Type a word:hello hello bye hello

Processing...


Traceback (most recent call last):
  File "/Python/glenn/aquaphoenix17.py", line 80, in <module>
    runCode()
  File "/Python/glenn/aquaphoenix17.py", line 74, in runCode
    print sentence
NameError: global name 'sentence' is not defined

#6

Sorry, I meant this:

def newLine():
    print ""

def error():
    print "An error has occured, please restart the program and try again."

def censor(text, word):
    if word in text:
        return text.replace(word, (len(word) * "*"))
    else:
        return text

def reverse(text):
    anti_text = ""
    for char in text[::-1]:
        anti_text += char
    return anti_text

def pyglatin(text):
    pyg = "ay"
    word = text.lower()
    first = word[0]
    new_word = word + first + pyg
    new_word = new_word[1:]
    if len(text) > 0:
        return new_word
    else:
        error()

def askQuestion(text):
    newLine()
    print text
    newLine()
    question_2 = raw_input("How about translating your text to pyglatin, yes or no?")
    if question_2 == "yes":
        new_2 = pyglatin(text)
        newLine()
        print "Translating..."
        newLine()
        print new_2
    elif question_2 == "no":
        new_2 = text
        newLine()
        print "Processing..."
        newLine()
        print new_2
    else:
        error()

def askQuestions(text):
    print "Censoring..."
    newLine()
    print text
    newLine()
    question_1 = raw_input("Would you like your text to be reversed, yes or no?")
    if question_1 == "yes":
        new_1 = reverse(text)
        newLine()
        print "Reversing..."
        askQuestion(new_1)
    elif question_1 == "no":
        new_1 = text
        newLine()
        print "Processing..."
        askQuestion(new_1)
    else:
        error()

def runCode():
    word = raw_input("Type a word:")
    newLine()
    print "Processing..."
    newLine()
    print word
    newLine()
    badWord = raw_input("What would you like to censor from the above text?")
    newLine()
    askQuestions(censor(word, badWord))

runCode()

#7

It works very well now. :sunglasses: ; cool.

Just one suggestion - In the runCode function, change this ...

    word = raw_input("Type a word:")

... to something like this ...

    word = raw_input("Type some text: ")

#8

Here is what runCode() looks like now:

def runCode():
    word = raw_input("Type some text: ")
    newLine()
    print "Processing..."
    newLine()
    print word
    newLine()
    badWord = raw_input("What would you like to censor from the above text?")
    newLine()
    askQuestions(censor(word, badWord))

#9
Type some text: good um... excellent um... awesome um... superb

Processing...

good um... excellent um... awesome um... superb

What would you like to censor from the above text?um...

Censoring...

good ***** excellent ***** awesome ***** superb

Would you like your text to be reversed, yes or no?yes

Reversing...

brepus ***** emosewa ***** tnellecxe ***** doog

How about translating your text to pyglatin, yes or no?yes

Translating...

repus ***** emosewa ***** tnellecxe ***** doogbay