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.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
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

0
Votes
1
Answer

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 Answer

365 Team
0
Votes

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

Thank you for answering my question!

1 month

You are very welcome!

4 weeks