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?
numbers = sorted(numbers)
tot = 0
while numbers[tot] < 20:
tot += 1
Also, I tried solving it without using the x[item] structure:
total = 0
total = total+1
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!
Thanks for reaching out.
Thank you very much for clearly describing the issues and providing a screenshot.
- 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.
- 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!
You are very welcome!