Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 26th, 2004, 10:34 PM
Questioner Questioner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Questioner's home
Posts: 89 Questioner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 32 m 10 sec
Reputation Power: 10
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.
__________________
What can change the nature of a man?

Last edited by Questioner : March 26th, 2004 at 10:38 PM.

Reply With Quote
  #2  
Old March 26th, 2004, 11:13 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Just int() the input and catch any ValueError you get from invalid int input.

Reply With Quote
  #3  
Old March 27th, 2004, 12:54 AM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 103 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
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?!?!?

Reply With Quote
  #4  
Old March 27th, 2004, 07:43 PM
TheBlackMamba TheBlackMamba is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 25 TheBlackMamba User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to TheBlackMamba Send a message via Yahoo to TheBlackMamba
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=....py&version=1.4

the download link will be up soon

Reply With Quote
  #5  
Old March 27th, 2004, 10:15 PM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 103 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
Sounds pretty sweet Mamba!

Please let us know when the download link becomes active.Thanks.

Reply With Quote
  #6  
Old March 29th, 2004, 11:24 PM
TheBlackMamba TheBlackMamba is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 25 TheBlackMamba User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to TheBlackMamba Send a message via Yahoo to TheBlackMamba
The link is working. Hope u like it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Integer input

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap