198k views
4 votes
Task1: #Define a function called show_students which takes 2 parameters students and message #Print out a message, and then the list of students #Next, put the students in alphabetical order and print them #Next, Put students in reverse alphabetical order and print them Example Output: Our students are currently in alphabetical order. - Aaron - Bernice - Cody Our students are now in reverse alphabetical order. - Cody - Bernice - Aaron

1 Answer

3 votes

Answer:

  1. def show_students(message, sList):
  2. print(message)
  3. print(sList)
  4. print("Our students are currently in alphabetical order")
  5. sList.sort()
  6. output = ""
  7. for student in sList:
  8. output += "-" + student
  9. print(output)
  10. print("Our students are currently in reverse alphabetical order")
  11. sList.sort(reverse=True)
  12. output = ""
  13. for student in sList:
  14. output += "-" + student
  15. print(output)
  16. show_students("Welcome to new semester!", ["Aaron","Bernice", "Cody"])

Step-by-step explanation:

Firstly we declare a function that will take two inputs, message and student list (Line 1).

In the function, we first print the message and the original input student list (Line 2 - 3). Next, we use sort method to sort the input list and then output the sorted items from the list using a for loop (Line 5-10).

Next, we sort the list again by setting reverse = True and this will sort the list in descending order (Line 13). Again we use the similar way mentioned above to output the sorted items (in descending order) using a for loop (Line 14 -17)

We test the function using a sample student list (Line 18) and we shall get the output:

Welcome to new semester!

['Aaron', 'Bernice', 'Cody']

Our students are currently in alphabetical order

-Aaron-Bernice-Cody

Our students are currently in reverse alphabetical order

-Cody-Bernice-Aaron

User Jetti Madhu
by
6.6k points