I'm writing a script that needs some very simple input from the user, and upon doing my research on this I am in a dilemma trying to work out the safest way to do it.
I only want an integer and stumbled across (what I thought was) a nice piece of code similar to (doing this from memory):
def getNumeric(prompt):
while True:
response = input(prompt)
try:
return int(response)
except ValueError:
print "please enter a number:",
This mostly worked, but if the user just pressed [enter] then it crashed. Turns out, I'm using python < 3, and the author wrote this for 3+. So now I have to rewrite for 2.7 to suit my needs. But how do I write it to cater for EITHER platform <3 and 3+?
I am writing something that could be used by others and want to make it the most portable I can. Should I bother?
As an aside, I am doing this for the Raspberry Pi, should I upgrade my python to 3, or leave it where it is (2.7.3)?