darrick Bledsoe
darrick Bledsoe

Reputation: 29

Creating a mirrored word based on symmetry of letter

I created this program that supposed to display the mirror image of a word if it is symmetrical. Unfortunately, when I test "TIMOTHY" it shows it cannot be mirrored and I can not figure out why this does not work.

#Program for creating a mirrored
#image word#
def main():

    mirrors = ["A","H","I","M","O","T","U","V","W","X","Y"
               "b","d","i","l","m","o","p","t","v","w","x"]

    word = input("Enter in the word you'd like mirrored: ")

    for x in word:
        if x not in mirrors:
            y = True
            break

    if y == True:
        print("Sorry your word can not be mirrored")


    wordlist = ''.join(word[i] for i in range(len(word) -1, -1, -1 ))

    print(wordlist)




main()    

Upvotes: 2

Views: 240

Answers (1)

Kevin
Kevin

Reputation: 76254

mirrors = ["A","H","I","M","O","T","U","V","W","X","Y"
               "b","d","i","l","m","o","p","t","v","w","x"]

Here's the problem. "b" follows "Y" without any intervening comma. A little-known quirk of Python is that two adjacent string literals will be automatically concatenated. So neither "Y" nor "b" are in your list, but "Yb" is.

To fix this, add a comma.

mirrors = ["A","H","I","M","O","T","U","V","W","X","Y",
               "b","d","i","l","m","o","p","t","v","w","x"]

Also, you should do y = False before your loop if you don't want to get an UnboundLocalError later during the if y == True block.

Upvotes: 5

Related Questions