Why do I get double spaces if the user has no middle name

0 votes

I'm trying to learn Python and this is my first script so please be patient with me :)

I ask the player for first_name, last_name and if they have a middle_name, if they do that is stored, if not it is None.

When they have a middle name there is a single space between first middle and last names. But, if they do not have a middle name I get first then two spaces then last. It's not a significant issue but it's bugging me and I want to understand why it is happening.

Here's my code so far:

first_name = input("What is your first name? ")
last_name = input("What is your last name? ")
while True:
    middle = input("Do you have a middle name? (y/n) ")
    if middle.upper() not in ("Y", "N"):
        print("whats does {} mean? You were meant to type 'y' or 'n'! Try again.." .format(middle))
    elif middle.upper() == "Y":
        middle_name = input("What is it? ")
        break
    elif middle.upper() == "N":
        middle_name = None
        break

# is_middle_empty = bool(middle_name)
# print(is_middle_empty)
print("So your full name is {} {} {}? ".format(first_name, '' if middle_name is None else middle_name, last_name))
import time
time.sleep(1)
print("Hmmm..")
time.sleep(1)
just_first = str(input("Should I just call you {}? (y/n) ".format(first_name)))
if just_first.upper() == "Y":
   player = first_name
   print("Great, nice to meet you", player)
elif just_first.upper() != "Y":
   name = first_name, "" if middle_name is None else middle_name, last_name
   player = " ".join(name)
   print("Sorry about that, let's stick to {} then." .format(player))
print()
adventure = input("So do you fancy a quick adventure? (y/n) ")
if adventure.upper() == "Y":
    print("Great then lets set off...")
elif adventure.upper() == "N":
    print("Ah well, I guess we can't all be ubercool adventurers like me, fairwell {}, I hope we meet again some day." .format(player))
Jan 27, 2021 in Python by Benn

edited Mar 4, 2025 291 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP