Noob Question: Force input to be int?

Daniel Jonsson not at set.se
Tue Jan 23 16:27:26 EST 2007


Ah, thank you for the respone! 
I have not gotten around to test it yet, but I hope it will work! :)
-Daniel 

2007-01-23 10:59:37
wd.jonsson at gmail.com wrote in message
<1169546377.017596.43840 at k78g2000cwa.googlegroups.com>

> Hello everyone!
> I have a piece of code that looks like this:
> 
> if len(BuildList) > 0:
>     print "The script found %d game directories:" % len(BuildList)
>     print
>     num = 0
>     for i in BuildList:
>         print str(num) +"       " + i
>         num = num + 1
>     print
>     print "Select a build number from 0 to " + str(len(BuildList) - 
1)
>     buildNum = int(raw_input('Select build #> '))
> 
>     while buildNum > (len(BuildList) -1) or buildNum <= -1:
>         print
>         print "Error: Invalid build number!"
>         print "Select a build number from 0 to " + 
str(len(BuildList) -
> 1)
>         print
>         buildNum = int(raw_input('Select build: '))
> 
> The problem is with the while buildNum-loop. If the user enters a
> non-numeric value in the buildNum input, the scripts throws an
> exception. I need to constrict the user to ONLY use integers in the
> input box. How can I solve this issue?



More information about the Python-list mailing list