Last answered:

25 Sept 2020

Posted on:

22 Sept 2020

0

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:

https://drive.google.com/file/d/1qPQvX7xx0O5vvrydS3o0N29XNYUz0uAB/view?usp=sharing

Thank you for your help!

1 answers ( 0 marked as helpful)
Instructor
Posted on:

25 Sept 2020

0
Hi! 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.
Best,
Martin

Submit an answer