Thread: Integer input

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Questioner's home
    Posts
    89
    Rep Power
    12

    Integer input


    I'm gonna accept numbers from users, and I do it like this :

    if this's gonna accept number 1 until 7 only :

    Code:
    choice = raw_input("Enter your choice : ")
    if '1' <= choice <= '7':
        if choice == '1':
            do something
        ...
    else:
        pass
    As you can see, I use raw_input() instead of input() to accept integers from user, so that it doesnt cause errors when someone accidentally key in a character. But I'm wondering, is there a faster way to do similar validation? Thanks in advance.
    Last edited by Questioner; March 26th, 2004 at 11:38 PM.
    What can change the nature of a man?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Just int() the input and catch any ValueError you get from invalid int input.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    I don't know if this is any faster,but it will loop the raw_input if the input is out of range,or if there's a ValueError(input is a character,not a number).I'm sure there's a better way to do it,but hopefully this will give you some ideas.

    Code:
    answer = raw_input('Enter a number.Valid range is 1-7:')
    
    while True: 
        try: 
            int(answer)
            while int(answer) not in range(1, 7): 
                answer = raw_input('\nInvalid number!Valid range is 1-7:')
                
            break
        
        except ValueError: 
            answer = raw_input('\nYou must specify a number!Valid range is 1-7:')
            
            
    if answer == '1': 
        print 'do response 1'
                               
    elif answer == '2': 
        print 'do response 2'
           
    elif answer == '3': 
        print 'do response 3'
    
    elif answer == '4': 
        print 'do response 4'
    
    elif answer == '5': 
        print 'do response 5'
    
    elif answer == '6': 
        print 'do response 6'
    
    elif answer == '7': 
        print 'do response 7'
    Code Block Generated With Py2Html
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    25
    Rep Power
    0

    Lightbulb


    Just fyi, I wrote a command line UI module with a spinbox widget that does exactly what you're trying to do.
    Here's the URL.
    http://www.python.org/pypi?:action=d...py&version=1.4

    the download link will be up soon
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    Sounds pretty sweet Mamba!

    Please let us know when the download link becomes active.Thanks.
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    25
    Rep Power
    0
    The link is working. Hope u like it.

IMN logo majestic logo threadwatch logo seochat tools logo