In Python, write a program that starts by making an empty list. Then the program
ID: 3696496 • Letter: I
Question
In Python, write a program that starts by making an empty list. Then the program must use a loop to prompt the user for the names of some notable celebrities and add each one to the list. Your loop must not predetermine how many celebrities will be added to the list. Instead, the loop should stop when the user enters "done" (NOTE: done must not be added to the list). The program should then output the number of celebrities entered. Next, the list should be sorted alphabetically. Finally, the program should use another loop to display the sorted celebrity names, each on its own line. SAMPLE OUTPUT Enter a celebrity name or done to quit Bono Enter a celebrity name or done to quit Ludacris Enter a celebrity name or done to quit Drake Enter a celebrity name or done to quit Adele Enter a celebrity name or done to quit Beyonce Enter a celebrity name or done to quit done Number of celebrities entered: 5 Here is your sorted celebrity list Adele Beyonce Bono Drake Ludacris
Explanation / Answer
celebrityList = [] #empty list
userInput = ""
#when the user enter 'done' the while loop ends
while userInput != "done":
userInput = raw_input("Enter a celebrity name or done to quit: ")
#next line is for don't save in the list if the user enter 'done'
if userInput != "done":
celebrityList.append(userInput) #add celebrity to list
#the function len() return the length of the list and we convert it to
#string with the function str()
print("Numbers of celebrities entered: " + str(len(celebrityList)))
sortedNames = sorted(celebrityList) #the function sorted() return the list sorted alphabetically
print("Here is your sorted celebrity list:")
for name in sortedNames: #now we take each element in the list and print it
print(name)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.