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


i cannot understand this exercise at all, i understood the previous ones but this code in the solution is difficult can u please help step by step?
def count(x):
x = sorted(x)
tot = 0

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

1 Answer

365 Team

Hi wahab!
Thanks for reaching out.
while loop works differently than a for loop. That’s why the structure of this solution is different from the one shown in the course, where we are using a for loop. 
To accommodate the solution to respond to the task while using a while loop, we need to sort the values first. The reason is that alternatively, Python will stop iterating once it encounters a value that doesn’t satisfy the given condition (which in our case is x[tot]<20). So, to make sure we’ve executed the loop for all values under 20, we need to sort them first!
Hope this helps but please feel free to get back to us should you need further assistance. Thank you.

Online Data Science Training
SAVE 60%