Create a function in Python to sort this list of high-scores in descending order. [150,…
- User
Create a function in Python to sort this list of high-scores in descending order. [150, 160, 233, 700, 800, 1200]
Assistant
Problem: Create a function in Python to sort this list of high-scores in descending order. [150, 160, 233, 700, 800, 1200]
Approach: with the
reverse=Trueparameter.Let me implement this solution.
Here is the Python code to achieve this.
We can use the built-in
.sort()method with thereverse=Trueparameter.def sort_high_scores(scores): # The sort() method modifies the list in-place # reverse=True sorts it from highest to lowest scores.sort(reverse=True) return scores # The list of high-scores high_scores = [150, 160, 233, 700, 800, 1200] # Calling the function sorted_list = sort_high_scores(high_scores) # Printing the result print(sorted_list)Output:
[1200, 800, 700, 233, 160, 150]