Nov-29-2018, 06:32 PM
Hi all, I'm new to python and trying to find the middle character of a word, how many characters the word has, and I need to print the letters that surround the middle character in lower case and all caps, I am struggling however with either 3 or 4 letter words. To get an example of my agenda I recommend you to try the word "house" with my code and you will see it works. However, words like "dog" or "door" do not print and I get the error "IndexError, string index out of range" Thanks for any help!
a = raw_input("Enter a Phrase: ") numb = len(a) mid = int(len(a) // 2) if numb == 3: mid = int(len(a)) if numb == 4: mid = int(len(a) // 2) else: threeup = a[mid] + a[mid + 1] + a[mid +2] threelow = a[mid] + a[mid + 1] + a[mid +2] print "There are " + str(numb) + " characters in this phrase" print "" + str(a[mid]) + " is the middle character" print threelow.lower() print threeup.upper()