G+_Tnt Seeker Posted May 14, 2014 Share Posted May 14, 2014 is there a way to make raw_input give me a int?? Link to comment Share on other sites More sharing options...
G+_jerry coffey Posted May 14, 2014 Share Posted May 14, 2014 hi sure num = int(raw_input("Please give me a number: ")) Link to comment Share on other sites More sharing options...
G+_L I Posted May 14, 2014 Share Posted May 14, 2014 problem with that is you risk getting an error if they don't type in a number. Try something like: response = raw_input("Please give me a number: ") while not response.isdigit(): response = raw_input("That was not a number. Please give me a number: ") num = int(response) Link to comment Share on other sites More sharing options...
G+_Tnt Seeker Posted May 14, 2014 Author Share Posted May 14, 2014 Thanks Link to comment Share on other sites More sharing options...
G+_L I Posted May 14, 2014 Share Posted May 14, 2014 One note - isdigit() only checks for positive whole numbers. If you want to check for negative numbers or decimals, you would have to use a try block. Link to comment Share on other sites More sharing options...
G+_Brandon Ingli Posted May 14, 2014 Share Posted May 14, 2014 isanint = false While isanint == false: Try: response = raw_input("Give me a Number") response = int(response) isanint = true Except: print "That was not a Number. Try Again" Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted May 14, 2014 Share Posted May 14, 2014 isdigit() returns true when its string argument contains at least one character, and all characters in the string are classified as digits by Unicode, which means they can be the standard 0..9, but also things like superscript digits, but not things like minus sign, decimal point or "e" that might otherwise appear in a valid number. The proper way to check for a valid number is simply to convert the string to a number inside a try/except block.? Link to comment Share on other sites More sharing options...
Recommended Posts