I have been working on a program which asks the user to type in a list of names. After they have typed in those names, my program has to right align all of those names. This is what I have so far:
names =[]
# User is prompted to enter a list of names
name = input ("Enter strings (end with DONE):\n")
while name != 'DONE':
names.append(name) # This appends/adds the name(s) the user types in, to names
name = input("")
print("\n""Right-aligned list:")
for name in names:
maximum = max(names, key=len) #This line of code searches for the name which is the longest
new_maximum = len(maximum) #Here it determines the length of the longest name
diff = new_maximum - len(name) #This line of code is used to subtract the length of the longest name from the length of another different name
title = diff*' ' #This code determines the open space (as the title) that has to be placed in front of the specific name
print(title,name)
Here is the program without all of the comments:
names =[]
name = input ("Enter strings (end with DONE):\n")
while name != 'DONE':
names.append(name)
name = input("")
print("\n""Right-aligned list:")
for name in names:
maximum = max(names, key=len)
new_maximum = len(maximum)
diff = new_maximum - len(name)
title = diff*' '
print(title,name)
The output I want for this program is:
Enter strings (end with DONE):
Michael
James
Thabang
Kelly
Sam
Christopher
DONE
Right-aligned list:
Michael
James
Thabang
Kelly
Sam
Christopher
Instead this is what I get:
Enter strings (end with DONE):
Michael
James
Thabang
Kelly
Sam
Christopher
DONE
Right-aligned list:
Michael
James
Thabang
Kelly
Sam
Christopher
NB:The moment the user types in DONE, the prompt ends.
The problem is that there is an extra space printed for every name in the list. How can I print it right-aligned but without the extra spaces?