Python Forum
Display middle character/s - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Display middle character/s (/thread-7970.html)



Display middle character/s - jmcatena91 - Jan-31-2018

OK, so this time I have a question about how to find the middle character/s. My instructions tell me for middle characters is

a. This operation will prompt the user for a choice between 'N' for number and 'S' for string
b. If the string has an odd number of characters
i. Determine the middle character of the string
ii. Display the single middle character to the user.
c. If the string has an even number of characters
i. Determine the middle characters of the string
ii. Display the two middle characters to the user.

I have everything done except this part. I can make it point to the middle characters for odd number numbers and strings but I dont want that, I want it to just display the letter/s and I have no clue where to start with 2 middle character strings. any info would be greatly appreciated!

The Middle Character is def MC and also commented with lable

## multi line comment to easily display the menu
a = '''
**************************
*         menu           *
**************************
* CC - Character Count   *
* RV - Reverse           *
* M5 - Multiply by 5     *
* MC - Middle Characters *
**************************
* EX - Exit              *
**************************
'''
print(a)


## Creating the individual functions for each choice.
## Character Count
def CC():
  cc = input('Enter string: ')
  print("'" + cc + "' is", len(cc), "characters long")
 
## Reverse
def RV():
  rv = input('Enter string: ')
  print(rv[::-1])

## Multiply by 5
def M5():
  m5 = input('choose "N" for number or "S" for string: ')
  if m5.lower() not in ('n', 's'):
    print('That is not a valid option.')
  elif m5.lower() == 'n':
    num = input('Enter a number :')
    print(num, '* 5 equals: ', int(num) * 5)
  elif m5.lower() == 's':
    sstr = input('Enter a string: ')
    print(str(sstr) * 5)

## Middle-Character
def MC():
  mc = input('choose "N" for number or "S" for string: ')
  if mc.lower() not in ('n', 's'):
    print('That is not a valid option.')
  elif mc.lower() == 'n':
## This is used for odd only.    
    text = input('Enter a number :')
    print(text)
    print('{0:^{1}}'.format('^',len(text)))
  elif mc.lower() == 's':
## This is used for odd only.
    text = input('Enter a string: ')
    print(text)
    print('{0:^{1}}'.format('^',len(text)))
 

## My function library
MyFunctions = {
    'cc': CC,
    'rv': RV,
    'm5': M5,
    'mc': MC,
}
 

## Function that actually calls the functions made at the beginning.
def pick_a_function():
    while True:
        func = input("Choose function 'CC', 'RV', 'M5', 'MC' or 'EX': ")
        if func.lower() == 'ex':
          print('Goodbye!')
          break
        elif func.lower() in MyFunctions:
            MyFunctions[func]()
        else:
            print('Invalid function, Try Again')
 
pick_a_function()



RE: Display middle character/s - wavic - Feb-01-2018

If the string length is even string[len(string)//2 - 1] is the first of the middle characters and string[len(string)//2] is the second. Just calculate the index.


RE: Display middle character/s - jmcatena91 - Feb-01-2018

(Feb-01-2018, 05:27 AM)wavic Wrote: If the string length is even string[len(string)//2 - 1] is the first of the middle characters and string[len(string)//2] is the second. Just calculate the index.

Thank you for your help, took a bit of tinkering and a lot of loud grunts but I did it. This is what if finally figured out.

## Middle-Character
def MC():
  mc = input('Enter String: ')
  if len(mc) % 2 == 0:
#even  
    print(mc[len(mc) // 2 - 1] + mc[len(mc) // 2])
  else: 
#odd
    print(mc[len(mc) // 2])