Repair infinite while loop

1

I have this program to solve within the resolution I have a loop while infinite and I do not know how to put it, because if I use a break I leave the funcion and I can not continue entering data to my program.

It has to be verified with the following example:

  • Input | Result

  • 6 | Value 1 = 6; multiple of 3

  • 10 | Value 2 = 10;
  • 6 | Value 3 = -6; multiple of 3

  • 10 | Value 4 = -10;

  • 0 | END: 2 multiples of 3

m=int(raw_input(""))
t=0
pares=0
while m>=0:
  t=t+1
  if m%2==0:
    pares=pares+1
    l="par"
  else:
    l="impar"
print "Valor",t,"=",m,";",l
    
asked by Miled 18.05.2018 в 07:35
source

2 answers

0

To be able to enter several numbers you need a while asking for numbers until one is negative, in this case the variable would be the entry.

def main():
    t=0
    pares=0
    m=int(input(""))
    if m>=0:
        if m%2==0:
            print('par')
        else:
            print('impar') 
        while m>=0:
          m=int(input(""))
          t=t+1
          if m%2==0:
            pares=pares+1
            l="par"
          else:
            l="impar"
          print(l)

main()

With this you guarantee that the numbers you enter and stores are greater than 0, keep in mind that when it comes to cycles and entries, you must be careful if you go inside or if you go outside, here you must be inside because it is the one that it will vary and the condition is as long as the entry is greater than or equal to zero.

    
answered by 18.05.2018 / 22:51
source
1

The loop is infinite because the output condition is marked by the variable m , which is never modified within the loop.

On the other hand, if you need to keep entering values, you need to put the text entry inside the loop and for the output to use, for example a specific character (like 0 for example), but, with the example you give, you should never check m as an exit condition.

    
answered by 18.05.2018 в 08:56