In: Computer Science
11. First and Last 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.
#Sentinel value is DONE
SENTINEL = "DONE"
ls = []
prompt = "Enter a name, or enter DONE to quit."
newLs = input(prompt)
n = newLs
#test for sentinel
while newLs != SENTINEL:
ls.append(newLs)
newLs = input(prompt)
#sort in alphabetical
for i in range(n) :
name = int()
ls.append(name)
ls.sort()
print('{} and {}'. format(ls[0],ls[n-1]))
I need help with sorting the names from the sentinel loop so that I
get only two outputs that are alphabetically ordered
As per your requirement, here is the Python code:
Rawcode:
#Sentinel value is DONE
SENTINEL = "DONE"
#creating a list to store the names
ls = []
#prompt variable which stores the string that says enter a name or enter done to quit
prompt = "Enter a name, or enter DONE to quit."
#prompting user for input name
newLs = input(prompt)
#while loop whcih runs until user enters DONE
while newLs != SENTINEL:
#appending each input name to the list
ls.append(newLs)
#keep on asking the user to enter the input name until user enters DONE
newLs = input(prompt)
#sort in alphabetical
ls.sort()
#as the list is sorted we have to print the first value of list and last value of list
#first value can accessed by using index value 0 and last value can be accessed by subtracting one from lenght of list
print('{} and {}'. format(ls[0],ls[len(ls) - 1]))
Here is the screenshot of source code:
Here is the screenshot of output:
Hope this helps you. Please feel free to ask if you still have any queries.
Do upvote. Thank you !