import string #importing some useful string methods """ This function returns True if my_string can be safely converted to an integer using the int function. """ 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 """ This function can be used as a substitute for the built-in Python input function, when we want to get an integer from the user. The difference is that the built-in function crashes if the user does not enter a number. The input_integer function just keeps asking the user to enter a number. """ def input_integer(message): while(True): text = raw_input(message) if (check_integer(text) == True): result = int(text) return result print "you need to type in an integer!" def list_to_string(list1): if not(type(list1) is list): return None result = "" for i in list1: if not(type(i) is str): return None if not(len(i) == 1): return None result = result + i; return result