F
F
falsem2020-07-15 11:36:28
Python
falsem, 2020-07-15 11:36:28

How to count the elements of a list in the desired numerical range?

Task:
Create a function comfort_count(temperatures) to count comfortable days in the passed list — days with air temperature from 22 to 26 degrees inclusive.
As a result of the function, the function should display the string 'Number of comfortable days this month: N', where N is the result of the calculation in the loop with the condition. First, count the comfortable days in May 2017, and then in May 2018.

The code:

may_2017 = [24, 26, 15, 10, 15, 19, 10, 1, 4, 7, 7, 7, 12, 14, 17, 8, 9, 19, 21, 22, 11, 15, 19, 23, 15, 21, 16, 13, 25, 17, 19]
may_2018 = [20, 27, 23, 18, 24, 16, 20, 24, 18, 15, 19, 25, 24, 26, 19, 24, 25, 21, 17, 11, 20, 21, 22, 23, 18, 20, 23, 18, 22, 23, 11]

def comfort_count(temperatures):
    temp = len(temperatures)
    for temp in temperatures:
        if 22 <= temp <= 26:
            temp += 1
    print('Количество комфортных дней в этом месяце: ', temp)

comfort_count(may_2017)  
comfort_count(may_2018)


As a result, the function displays the last element of the list, what's wrong?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
D
Dmitry, 2020-07-15
@falsem

No need to use temp for counting days and for iterating.

count = 0
for t in temperatures:
   if t <= 26:
      count += 1

S
Sergey Gornostaev, 2020-07-15
@sergey-gornostaev

The loop variable overwrites the variable in the same scope.

A
Andrey, 2020-07-15
@anerev

def comfort_count(temperatures):
    print('Количество комфортных дней в этом месяце: ', len([x for x in temperatures if x <= 26 and x >= 22]))

A
Anton, 2017-03-07
@Prakop

Here's what helped me, insert this meta tag into the header of the site

<meta name="viewport" content="width=device-width, initial-scale=1">

V
vanya novikov, 2014-12-15
@illinifellow

as a rule, such difficulties arise when the specificity of such an appeal to selectors is not taken into account. try throwing these properties at the end of the css file.
Without getting into the details of the specific problem, there are a few steps to identifying the error:

  • check specificity (above)
  • see in the browser in firebug what happens and what is applied to the elements depending on the circumstances
  • try to test individual simple pieces of code and see if anything works. For example like this:
    @media all and (min-width: 640px) and (max-width: 734px){
        * {
           background: red;
        }
    }

S
Sergey Zolotarev, 2018-07-12
@seregazolotaryow64

I tried to apply resolutionand device-widthattached the scaling of the whole body to them. Applied @media screenand @media allconnected the tag viewport. I'm testing on a netbook and nothing works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question