DEV Community

loading...

Discussion on: Daily Challenge #38 - Middle Name

Collapse
teotcd profile image
JedDevs

Python.

I had two attempts at this, my first was only for full names up to three words but after realising I needed to expand the program to work on more I rewrote the program. Both and posted below and annotated.

First attempt

def initialize(full_name):
  names = full_name.split()
  num_names = len(names)
  if num_names == 3:
    Middle_initial = names[1]
    print(names[0],Middle_initial[0],".",names[2])
  elif num_names <=2:
    print(names[0],names[1])
  else:
    print("Compatability for names of 4 or more has not been added.")
full_name = input("Full Name: ")
if not full_name == "":
  initialize(full_name)

Second Attempt

def initialize(full_name):
  start = 1
  names = full_name.split()
  num_names = len(names)
  if num_names >= 3:
    answer = ""
    change = num_names-1
    while not start == change:
      a = names[start]
      a = a[0]
      start = start + 1
      answer = answer + a.upper() + "."
    print(names[0],answer,names[num_names-1])
  else: print(full_name)
full_name = input("Full Name: ")
if not full_name == "":
  initialize(full_name)

I wasn't too happy on the result,
Any pointers on how to I could improve my code would be great!

Collapse
teotcd profile image
JedDevs

But ey it works,

Python 3.7.4 (default, Jul  9 2019, 00:06:43)
[GCC 6.3.0 20170516] on linux
Full Name: Ben Dark Aye Daggity do yee Dover
Ben D.A.D.D.Y. Dover

😁..I am uh, very, mature.