The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Anybody can ask a question
Anybody can answer
The best answers are voted up and moderated by our team

Exercise: All in – Conditional Statements, Functions, and Loops

Exercise: All in – Conditional Statements, Functions, and Loops


Hi, in the exercise for the lesson “All in”, I could not figure out the answer using the while loop. I checked the solution, but I still don’t understand the code. What does “numbers = sorted(numbers)” mean?

def count(numbers):
numbers = sorted(numbers)
tot = 0

while numbers[tot] < 20:
tot += 1
return tot


Also, I tried solving it without using the x[item] structure:

def count(numbers):
total = 0
while total<20:
total = total+1
return total

But the output is only 1.

For some reason, indentation is not shown after submitting the question. So I have attached a screenshot of my work:

Thank you for your help!

1 Answer

365 Team

Thanks for reaching out.
Thank you very much for clearly describing the issues and providing a screenshot.

  1. sorted() is a Python built-in function that sorts the values from the provided argument in ascending order. When we use numbers = sorted(numbers), we are using an assignment statemnt to overwrite the content of the initial numbers variable. The new content is the same numbers, arranged in ascending order.
  2. We are sorry you could not display indentation directly. Thank you for pointing this out. 
    Otherwise, we need the x[item] structure to refer to the elements from the given iterable (i.e. sequence through which we are looping, so to speak). When you use total directly, you are referring to the given number (as you set it equal to 0, you are basically referring to… the number 0). This changes the way the iteration will operate.
    Regarding the indentation – it is also a matter of syntax, so please stick to the code suggested in the course regarding this point.

Hope this helps but please feel free to get back to us should you need further assistance.

Thank you for answering my question!

4 months

You are very welcome!

4 months
Complete Data Science Education
Get 50% OFF