Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with code
#1
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()
Reply
#2
To see the error think about mid. For a three letter word, mid is 3 (line 5). So you try to get a[3], a[4], and a[5] on line 10. Being three letters long, the only valid indexes for a are a[0], a[1], and a[2]. For a four letter word, mid is 2 (line 7). Then you're getting a[2], a[3], and a[4], on line 10. But the highest you can go is a[3].

Also, lines 10 and 11 are getting the middle character and the two characters after it, not the two characters around it. I would change that to a[mid - 1] + a[mid] + a[mid + 1], or better yet use a slice: a[mid - 1:mid + 2]. Also, you should change line 5 to mid = len(a) // 2 - 1. You might want to use that for all words, but it is not clear how your assignment is defining the middle letter of a four letter word. In 'code' is the middle letter 'o' or 'd'?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hi, thanks for the reply, I apologize as it does not need surrounding characters but the following two. Example, house would generate USE for the threeup. I found your other tips to be successful and thank you, however for line 5, it does not print the middle character but it prints the first character (when inputting "dog", it states that the middle character is "d".) Thanks for any help you can offer, below is the new code, I apologize in advance if I do not reply soon, as I have to leave my PC for a couple hours while I leave in around 10 minutes. Thanks in advance.

a = raw_input("Enter a Phrase: ")
numb = len(a)
mid = int(len(a) // 2)
if numb == 3:
    mid = (len(a) // 2 - 1)
    threeup = a[mid - 1] + a[mid] + a[mid + 1]
    threelow = a[mid - 1] + a[mid] + a[mid + 1]
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()
Reply
#4
Yeah, I though that through wrong. They should both be len(a) // 2. So you don't need lines 4-9. Also, you don't need both threeup and threelow. They're the same thing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020