[Tutor] Re: Checking for int only

K J game at gameweave.com
Mon Jun 28 17:21:31 EDT 2004

I tried what you said and it work good but when you picked a different  char
other than an int it would just say Please choose an number: and if you
messed up twice then it would say that is not an option. So I did some
fooling around with it and came up with this so that it will say that is not
an option the first time.
So thanks for the help.

number = raw_input("Please choose an number: ")

while True:
        number = int(number)
        print '  That is not an option.'
        number = raw_input("Please choose an number: ")

----- Original Message -----
From: Andrei <project5 at redrival.net>
To: <tutor at python.org>
Sent: Monday, June 28, 2004 3:13 PM
Subject: [Tutor] Re: Checking for int only

> K J wrote on Mon, 28 Jun 2004 15:07:44 -0400:
> > How do you test for something to make sure that it is an int and not
anything else
> > so that when you ask them to enter a number they can only enter a number
> > not any other character.
> Use try-except:
> while True:
>     number = raw_input('Integer: ')
>     try:
>         number = int(number) # will fail if the input is non-numerical
>         break # stop endless loop
>     except: # int() failed -> not an integer
>         print '  That was not an integer.'
> Above is assuming that you mean 'integer' when you write 'number'. If you
> mean to include floats, try float() instead of int().
> --
> Yours,
> Andrei
> =====
> Real contact info (decode with rot13):
> cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
> gur yvfg, fb gurer'f ab arrq gb PP.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list