lundi 4 mai 2015

Python 3: if/else statement skips straight to else [SOLVED]

Alright, so I was working on a Pig Latin translator (like the one in the Codecademy lessons, but more advanced), and it works, except for when I prompt the user if they want to translate another word or not, but the if/else statement that I give the user always skips to the else no matter what the input. The module gives no syntax errors or anything, and I don't see ANYTHING wrong with the code. This is the block from my program that is giving me issues:

def translator():
    job = input("Are you translating TO or FROM PygLatin? ")
    if job.upper() == "TO":
        word = input("Alright, what's the word we're translating? ")
        wordlength = len(word)
        firstletter = word[0]
        choppedword = word[1:wordlength]
        completeword = choppedword + firstletter + pyg
        print("Okay! Your translated word is...")
        time.sleep(3)
        print(completeword)
        time.sleep(3)
        redo = input("Wanna translate something else? Y/N ")
        if redo.upper == "Y":
            print("Awesome!")
            time.sleep(3)
            translator()
        else:
            print("Oh, okay then... Bye " + name + "!")
            sys.exit()
    elif job.upper() == "FROM":
        word = input("Alright, what’s the word we’re translating? ")
        wordlengthtwo = int(len(word))
        firstletterindex = int(wordlengthtwo - 3)
        firstletter = word[firstletterindex]
        choppedword = word[0:firstletterindex]
        newword = str(firstletter.upper() + choppedword)
        print("Okay! Your translated word is...")
        time.sleep(3)
        print(newword)
        time.sleep(3)
        again = input("Wanna translate something else? Y/N ")
        if again.upper == "Y":
            print("Awesome!")
            time.sleep(3)
            translator()
        else:
            print("Oh, okay then... Bye " + name + "!")
            sys.exit()

More specifically, these parts in the coding:

redo = input("Wanna translate something else? Y/N ")
if redo.upper == "Y":
    print("Awesome!")
    time.sleep(3)
    translator()
else:
    print("Oh, okay then... Bye " + name + "!")
    sys.exit()

And,

again = input("Wanna translate something else? Y/N ")
if again.upper == "Y":
    print("Awesome!")
    time.sleep(3)
    translator()
else:
    print("Oh, okay then... Bye " + name + "!")
    sys.exit()

So... Any ideas on why this may be happening or how to fix it?

Aucun commentaire:

Enregistrer un commentaire