import string #importing some useful string methods def check_integer(my_string): # this line removes whitespace from the beginning and the end. my_string = string.strip(my_string) position = 0 # we will set illegal_flag to true if we find proof that the string is # not a valid integer illegal_flag = False # skip any minus sign at the beginning if position < len(my_string): character = my_string[position] if character == "-": #skip minus sign position = position + 1 # we now should get a string of consecutive digits. Let's count how many. digit_count = 0 while position < len(my_string): character = my_string[position] if character in "0123456789": position = position + 1 digit_count = digit_count + 1 else: illegal_flag = True break if digit_count == 0: illegal_flag = True if (illegal_flag == True): return False else: return True