-5

Why does this code repeatedly take input and never terminate? I want to know how is the input related to the condition inside the while loop. The input command is outside the while loop so it should be independent of the condition.

n = input()
n = int(n)


count = 0
while n > 0:
    if n% 10 ==0:
        n =n/10
        count = count +1
    else :
        n = n+1
        count = count +1
  • Use a `break` statement. – Arkistarvh Kltzuonstev Apr 26 '19 at 16:30
  • Because `while n > 0` always `True`. I just tested for input value `1`. You can explain what you wanted to do? – shafik Apr 26 '19 at 16:30
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – G. Anderson Apr 26 '19 at 16:32
  • If while is satisfied everytime then why does input is infinite. I used input command just once. Am i missing something? Input command in outside the while loop so it should not depend on it. – Harshal Sinha Apr 26 '19 at 16:36
  • 2
    Is that your whole code? The code you posted does *not* take input more than once. – Blorgbeard Apr 26 '19 at 16:37
  • Your while loop will run forever though. – Blorgbeard Apr 26 '19 at 16:38
  • Yes i ran it for every possible input and it forms an infinite loop except for 0 – Harshal Sinha Apr 26 '19 at 16:39
  • @Blorgbeard Sir, I want to know why is it forming infinite loop. My input command is outside the while loop. So how is it depending on the while loop condition – Harshal Sinha Apr 26 '19 at 16:41
  • It's only accepting input once though, right? Your question states that "this code takes input infinite times". – Blorgbeard Apr 26 '19 at 16:57
  • What are you even trying to do with this code? What problem are you solving? – Blorgbeard Apr 26 '19 at 16:57
  • Did you print the intermediate results (`n` and `count`) in the loop to understand what's happening? Did you use a debugger to step through your code? – wovano Apr 28 '19 at 09:03
  • I want to know how is the input related to the condition inside the while loop. The input command is outside the while loop so it should be independent of the condition. – Harshal Sinha Apr 28 '19 at 14:23
  • The line where i mentioned n = input(). So here n is a constant ,right? So the value of n should get passed in the while loop. And by the answers I am getting it looks like the variable n is the input command which gets passed inside the while loop. – Harshal Sinha Apr 28 '19 at 14:27
  • 2
    I'm sorry, your question is not understandable. Please explain **exactly** what behaviour you expect from this code, and then explain **exactly** what behaviour you see instead. And answer all the questions people have asked in comments. – Blorgbeard Apr 28 '19 at 23:08

1 Answers1

0

As long as the result of a sum of two positive values is greater than each of the values and a division can't give you zero if the dividend is a non-zero value, your program will always end on an infinite loop unless n=0 or n<0. Either you are in the first or in the second case, count will always be 0.

Post your code or at least explain what you're trying to achieve in order to get a better answer.

F. Malato
  • 192
  • 2
  • 15