About improper input


In the code below , if the player use numbers as the input , it will successfully turn the size (string) to datatype number , but if the player type something other than numbers, it will cause the program error.
How can the program avoid this case happen? I have thought about using the command type() to avoid this mistake but i don't think it work.So how can we test for these problem.

Sorry for my poor English, I am just a student in Hong Kong , so my English is not well.

size = raw_input("select the size of the map")
size2 = int(size)


Well you typed in a number, which is stored in size, and will turn into an int in size2. but when you type in a word, you can't turn words into ints, so that's where your error is coming from. Letters can't be numbers. (int())


This post here covers what you are trying to do.


try/except is the way to go, it allows you to handle errors

its not covered in this python course

you can read the python docs:


that is a coincidence, the first error handling shown by the doc is exactly the problem you are experiencing.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.