Jump to content

is there a way to make raw input give me a int?


G+_Tnt Seeker
 Share

Recommended Posts

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

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

 Share

×
×
  • Create New...