0

When I insert John J Doe Dole into input as the providedname, the code repeats the first 3 print statements and then displays the last 3 print statements. So, I end up having 9 names instead of the intended 6. It executes all other name combinations perfectly, except for this one (I left the rest of the code out). The purpose of this code is to display all name combinations with a provided name, regardless if its a 2, 3 or 4 word name, as in John Doe, John Joe Doe or John Joe Doe Dole.

print ('Please type the name for naming combinations to be conducted.')
providedname = input()

if len(providedname.split())==4:
    firstname, middlename, lastname, secondlastname = providedname.split()

#Provides combination if both the middlename and second lastname have more than 1 character
    if len(middlename) and len(secondlastname) > 1:
        print ('Combinations for databases with no syntax:')
        print (firstname + ' ' + middlename + ' ' + lastname + ' ' + secondlastname)
        print (firstname + ' ' + middlename + ' ' + lastname + ' ' + secondlastname[:1])
        print (firstname + ' ' + middlename + ' ' + lastname)
        print (firstname + ' ' + middlename[:1] + ' ' + lastname + ' ' + secondlastname)
        print (firstname + ' ' + middlename[:1] + ' ' + lastname + ' ' + secondlastname[:1])
        print (firstname + ' ' + middlename[:1] + ' ' + lastname)
        print (firstname + ' ' + lastname + ' ' + secondlastname)
        print (firstname + ' ' + lastname + ' ' + secondlastname[:1])
        print (firstname + ' ' + lastname)   
#Provides combination if the middle name is equal to 1 character and the second last name more than 1 character
    elif len(middlename) == 1 and len(secondlastname) > 1:
        print (firstname + ' ' + middlename + ' ' + lastname + ' ' +   secondlastname)
        print (firstname + ' ' + middlename + ' ' + lastname + ' ' + secondlastname[:1])
        print (firstname + ' ' + middlename + ' ' + lastname)
        print (firstname + ' ' + lastname + ' ' + secondlastname)
        print (firstname + ' ' + lastname + ' ' + secondlastname[:1])
        print (firstname + ' ' + lastname)
    else:
        pass
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
adriaanbd
  • 317
  • 4
  • 12
  • `if len(middlename) and len(secondlastname) > 1:` should be `if len(middlename) > 1 and len(secondlastname) > 1:` – MooingRawr Oct 26 '16 at 17:28
  • To expand on MooingRawr, you're always succeeding on the first if statement, because `len(middlename)` is equal to `1` when `middlename="J"`, which is truthy, and `len(secondlastname) > 1` is true when `secondlastname="Dole"`. – user3030010 Oct 26 '16 at 17:38
  • Thank you very much, that indeed solved my issue! – adriaanbd Oct 26 '16 at 18:30

0 Answers0