аватар question@mail.ru · 01.01.1970 03:00

While cycle "Summer of the number of numbers"

Python began to study, sometimes terrible stupids attack.
A number of tasks with this cycle, it seems, is solved and understandable,
I can’t understand how to formulate the solution?

Determine the sum of all elements of the sequence of 0.
in this and next tasks of the number following the zero, not the following. They are taken into account.
Def there was no one on this course, I do not know what it is.
i.e. The problem must be solved in the framework of the studied material:

  1. input-output, arithmetic operations
  2. conditional instruction
  3. operations with entire and material numbers
  4. cycle for
  5. lines
  6. the While
  7. that's just what I did not understand.

      I =  0    when   int  ( input  () & gt;  0 :  i +=  int  ( input  ()   print  (i)     

    but in this case elements are summarized without the first,
    i.e. In the order, as included.

    if I do this,

      i =  0  a =  0    when   int  ( input  () & gt;  0 :  i += "  1    for  j  in   range  ( 0 : a):  a +=  int  ( input  ())   print  (a)     

    then the numbers for the input.
    In general, I understand that I am doing something wrong.

аватар answer@mail.ru · 01.01.1970 03:00

The mistake is that the function input () is caused in two places, you need to call it only in one place and use the same retu value both for checking for zero and for summing:

  TOTAL =  0  # total    when  : # Endless cycle   n =  int  ( input  ()) # each line contains an integer    if  n ==  0 : # Found zero    break  # "leave the cycle   tutal += n # Summarize    print  (total) # print the result      

the sequence ends with zero. Otherwise, you need to catch eoferror exception and process the retued empty line.

In order to independently find an error in your code, in such simple cases, it is useful to write out in words that the code makes the line behind the line (debugging). Improving the understanding of the task of people who already know the python is not for direct use):

   import  sys  from  itertools  import  takewhile  print  su  (takeewhile ( lambda  n: n! =  0 ,  map  ( int , sys.stdin))))     

non -zero integers are the truth in the brown context in the python, so instead of
lambda n: n! = 0
you can use simply bool (less readable (obvious) Option).

or even (if each line contains only numbers, without spaces):

   Print  ( sum  ( map  ( int ,  it  ( input ,  '0' ))))                                       

Latest

Similar