Integer.parseInt(scanner.nextLine()) vs scanner.nextInt()

There are 2 observations : Using myScannerInstance.nextInt() leaves behind a new line character. So, if you call nextLine() after nextInt(), the nextLine() will read the new line character instead of the actual data. Consequently, you will have to add another nextLine() after the nextInt() to gobble up that dangling new-line character. nextLine() doesn’t leave behind … Read more

Stop data inserting into a database twice

I call this a golden rule of web programming: Never ever respond with a body to a POST-request. Always do the work, and then respond with a Location: header to redirect to the updated page so that browser requests it with GET. This way, refreshing will not do you any harm. Also, regarding a discussion … Read more

Identifying the data type of an input

from ast import literal_eval def get_type(input_data): try: return type(literal_eval(input_data)) except (ValueError, SyntaxError): # A string, so return str return str print(get_type(“1”)) # <class ‘int’> print(get_type(“1.2354”)) # <class ‘float’> print(get_type(“True”)) # <class ‘bool’> print(get_type(“abcd”)) # <class ‘str’>

Limiting Python input strings to certain characters and lengths

Question 1: Restrict to certain characters You are right, this is easy to solve with regular expressions: import re input_str = raw_input(“Please provide some info: “) if not re.match(“^[a-z]*$”, input_str): print “Error! Only letters a-z allowed!” sys.exit() Question 2: Restrict to certain length As Tim mentioned correctly, you can do this by adapting the regular … Read more