Displaying the first name alphabetically and last name alphabetically after being given a list of names from the user in python
Question:
Design a program that asks the user for a series of names (in no particular order). After the final person’s name has been entered, the program should display the name that is first alphabetically and the name that is last alphabetically. For example, if the user enters the names Kristin, Joel, Adam, Beth, Zeb, and Chris, the program would display Adam and Zeb.
Note: A condition-controlled loop must be used. The user will enter a sentinel value of DONE to indicate that there are no more names to be input.
We really haven’t gone over this in class and I’ve tried looking it up online to see if I can understand anything but most of the people are asking about it in java or C++.
My Code:
# Prompt user to enter names
names = input("Enter the names: ")
# breakdown the names into a list
words = names.split()
# sort the names
words.sort()
# display the sorted names
print("The sorted names are:")
for word in words:
print(word)
Answers:
Here is a code that will solve your problem:
print("Keep entering names by typing space bar after every name. Once done, press Enter")
final_list = list(map(str, input().rstrip().split()))
final_list.sort()
print("The first name is {}. The last name is {}".format(final_list[0],final_list[-1]))
Best way to learn is to try it yourself. Most import things is (imho) to read the assignment carefully and then break it down in smaller, but easier to solve problems:
# init names list
names = []
# get input
while True:
name = input("Enter a name:")
# end while loop, when name = 'DONE'
if name == "DONE":
break;
else:
# else add it to 'names' list
names.append(name)
# Do some sorting of the list
names.sort()
# Print the first item of a list
print(names[0])
# Print the last item of a list
print(names[-1])
Start
Declare String name
Display “Enter a name.”
Input name
if name=A then
Display “The first name is”,name
else if name=Z then
Display “The last name is”,name
end if
End
Design a program that asks the user for a series of names (in no particular order). After the final person’s name has been entered, the program should display the name that is first alphabetically and the name that is last alphabetically. For example, if the user enters the names Kristin, Joel, Adam, Beth, Zeb, and Chris, the program would display Adam and Zeb.
Note: A condition-controlled loop must be used. The user will enter a sentinel value of DONE to indicate that there are no more names to be input.
We really haven’t gone over this in class and I’ve tried looking it up online to see if I can understand anything but most of the people are asking about it in java or C++.
My Code:
# Prompt user to enter names
names = input("Enter the names: ")
# breakdown the names into a list
words = names.split()
# sort the names
words.sort()
# display the sorted names
print("The sorted names are:")
for word in words:
print(word)
Here is a code that will solve your problem:
print("Keep entering names by typing space bar after every name. Once done, press Enter")
final_list = list(map(str, input().rstrip().split()))
final_list.sort()
print("The first name is {}. The last name is {}".format(final_list[0],final_list[-1]))
Best way to learn is to try it yourself. Most import things is (imho) to read the assignment carefully and then break it down in smaller, but easier to solve problems:
# init names list
names = []
# get input
while True:
name = input("Enter a name:")
# end while loop, when name = 'DONE'
if name == "DONE":
break;
else:
# else add it to 'names' list
names.append(name)
# Do some sorting of the list
names.sort()
# Print the first item of a list
print(names[0])
# Print the last item of a list
print(names[-1])
Start
Declare String name
Display “Enter a name.”
Input name
if name=A then
Display “The first name is”,name
else if name=Z then
Display “The last name is”,name
end if
End