1

I am a beginner in Python and I was trying to write a small program that asks the user to enter an integer that is greater than 0. The function should keep on asking the user for the number until it is valid.

I tried something like below, however I am getting the wrong results. Can you please help me understand my error?

num = input('please enter a number: ' )
n = int(num)
while n < 0:
    num = input('please enter a number: ' )
    if n >= 0:
       print('Valid number')
    else:
       print('Invalid number') 

Is it possible to start the code without an input function? (like to start with num = int())

Thank You for Your Time

moinabyssinia
  • 163
  • 1
  • 2
  • 9

2 Answers2

1

If your problem is the code not terminating, writing Invalid number all the time it's because you are not updating the value of n. You assigned it just once. The solution to your problem is as follows:

n = -1
while n < 0:
    n = int(input('please enter a number: '))
    if n >= 0:
        print('Valid number')
    else:
        print('Invalid number')

By the way you get rid of starting the code without an input function.

Edit:

As you just said - you want to keep the input reading despite passing an negative integer into the command line. This should help you accomplish this:

while True:
    n = int(input('please enter a number: '))
    if n >= 0:
        print('Valid number')
    else:
        print('Invalid number')

This loop will go forever until you exit the program lets say with ctrl + C. while True: is as you see an forever ongoing loop, because the True argument will never be false.

Adrian Jałoszewski
  • 1,695
  • 3
  • 17
  • 33
1

There's an error with the logic behind your code.

  1. You firstly ask the user for a number and if he inputs a number which is greater than or equal to 0, the while-loop will never start (in your script: while n < 0:), which, I assume is fine, because the goal of your program is, as you said, to make "the user to enter an integer that is greater than 0".

  2. If the user inputs a number that is smaller than or equal to 0, the while-loop will start, but will never break because inside of it, the value of variable n never changes, only does the value of num.

This is an appropriate script considering that you want to make the user input a number greater than 0, and that you want to give feedback regarding their input.

n = None

while n <= 0:

    n = int(input('please enter a number: '))

    if n <= 0:
        print('Invalid number')

    else:
        pass # the loop will break at this point
             # because n <= 0 is False

print('Valid number')

The code has the user stuck in a loop until they write a number that's greater than 0.

Another solution would be to, inside the loop, check whether int(num) is greater than 0 and if it is, print 'Valid number' and do break to stop the loop; if it's not, print 'Invalid number' (though then the loop doesn't need to be defined by while n < 0:; rather by while True:.

Also, what do you mean by this:

Is it possible to start the code without an input function? (like to start with num = int())

Please clarify this part.

  • Thank you dear for your clear answer. What I mean by that is, I understood that I replicated the input question(please enter a number). I wanted not to replicate. And if I dont write anything in the beginning, before the while loop, Python gives error. So I wanted to not replicate the input question. – moinabyssinia Aug 19 '16 at 16:09