Last answered:

10 Nov 2020

Posted on:

08 Nov 2020

0

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

hello, 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 answers ( 0 marked as helpful)
Instructor
Posted on:

10 Nov 2020

0
Hi wahab! Thanks for reaching out. A 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.
Best,
Martin

Submit an answer