0

What is the best way of repeatedly asking for the user for correct input?

For example I'd like to continue checking if the a value is an int or not and when it is finally say ok you picked number.

I'm stuck here:

    try:

      a = int(input())

    except:
       print("incorrect pick a number and try again")


#Somewhere here

print("Ok thanks you finally picked a number")
    
Carlo Zanocco
  • 1,967
  • 4
  • 18
  • 31
Frank
  • 11
  • 1

4 Answers4

1

The only exception you want to catch is the ValueError raised by int should it not be able to convert its argument to an integer. The try statement will be in a loop that you will explicitly break out of if you don't get an exception.

while True:
    response = input()
    try:
        a = int(response)
    except ValueError:
        print("incorrect pick a number and try again")
    else:
        print("Ok thanks you finally picked a number")
        break
chepner
  • 497,756
  • 71
  • 530
  • 681
0

You can do it like this:

a = None
while type(a) != int:
    try:
        a = int(input())
    except:
        print("incorrect pick a number and try again")

print("Ok thanks you finally picked a number")

Essentially:

  • Create a variable with NoneType so when you first run the program it can access the while loop because the conditions meats.
  • Loop until the type of that variable is not int.
  • Request the user input and try to cast as integer, if it fails print an error and try again.
  • When the input is of integer type it exit the loop and print the final message.

You can use type() or isinstance() to check, but as suggested by @chepner avoid the usage of type because it simply returns the type of an object whereas, isinstance() returns true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof.

To help you understand:

class foo:
    pass

class bar(foo):
    pass

isinstance(foo(), foo)  # returns True
type(foo()) == foo      # returns True
isinstance(bar(), foo)    # returns True
type(bar()) == foo        # returns False,and this probably won't be what you want.

In the case you want to use isinstance() the code will result as:

a = None

while not isinstance(a, int):
    try:
        a = int(input())
    except:
        print("incorrect pick a number and try again")

print("Ok thanks you finally picked a number")

As pointed out by @L3viathan you can also do:

a = None
while a is None:
    try:
        a = int(input())
    except:
        print("incorrect pick a number and try again")

print("Ok thanks you finally picked a number")

In this case you loop until a get a value, that will be done only if the exception is not thrown.

Carlo Zanocco
  • 1,967
  • 4
  • 18
  • 31
  • How would `type(a)` ever be anything other than `int` or `type(None)`? Why not do `while a is None:` instead in that case? – L3viathan Sep 26 '20 at 15:30
  • You almost never want to use `type(x) == y`; use `isinstance(x, y)` instead. – chepner Sep 26 '20 at 15:37
  • @chepner explain why please. As I know type() simply returns the type of an object whereas, isinstance(): returns true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. – Carlo Zanocco Sep 26 '20 at 15:38
  • @CarloZanocco That's precisely why. You almost never care about the *exact* type, ignoring sub typing. – chepner Sep 26 '20 at 15:40
  • @chepner In this case how it could be a problem? – Carlo Zanocco Sep 26 '20 at 15:42
  • It's a bad habit to get into. Unless you have a *reason* for ignoring subtypes, don't do it. – chepner Sep 26 '20 at 15:44
  • @chepner Thanks, I have updated the question to also explain to the other – Carlo Zanocco Sep 26 '20 at 15:52
0
while True:
    try:
        number=int(input("Enter a number: ")) 
    except:
        print("incorrect pick a number and try again")
    else:
        print("ok Thanks you finally picked  a number ")
        break
  • `number` is, by definition, an `int` if you reach the statement following the `try` statement. You don't need `isinstance`. – chepner Sep 26 '20 at 15:35
0

if you want/must mantain this code, i think the unique way is using a loop.

while True:
try:
    a = int(input('Insert a number:'))
    print ('Your number is {}'.format(a))
    break

except ValueError:
    print("incorrect pick a number and try again")

So if the user inserts an integer, the code prints number and break the loop else repeats the request. I hope it's useful for you!