By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,403 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,403 IT Pros & Developers. It's quick & easy.

raw input and input

100+
P: 176
Hello. I was wondering if it was possible to have a input be both a raw_input and an input so that way errors are not causes if the following happened:

>>>number = input()
blah
Error Message
Apr 1 '08 #1
Share this Question
Share on Google+
1 Reply


jlm699
100+
P: 314
As a rule you should only use raw_input. If you require a number than you are able to verify via string methods as in the following example.
Expand|Select|Wrap|Line Numbers
  1. def check_numeric(inp):
  2.         if inp.isdigit():
  3.             print "Thank you for entering %s" % inp
  4.         else:
  5.             print "Your input of %s is not a number :(" % inp
  6.  
  7. >>> usr_inp = raw_input("Enter a number: ")
  8. Enter a number: 55
  9. >>> check_numeric(usr_inp)
  10. Thank you for entering 55
  11. >>> usr_inp = raw_input("Enter a number: ")
  12. Enter a number: blah
  13. >>> check_numeric(usr_inp)
  14. Your input of blah is not a number :(
  15. >>> 
Apr 2 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.