""" This program: - asks the user to enter a string. - verifies that the string represents an integer, and has the format: - [optional] white space at the start - [optional] minus sign - a sequence of consecutive characters of digits, from 0 to 9. - [optional] white space at the end This is a simpler implementation of verify_integer1.py, with the same functionality, by stripping white space from the beginning and end of the string. """ import string #importing some useful string methods my_string = raw_input("enter a string: ") # this line removes whitespace from the beginning and the end. my_string = string.strip(my_string) print "checking if", my_string, "is an integer..." 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 print "illegal character found:", character break if digit_count == 0: illegal_flag = True if (illegal_flag == True): print "String", my_string, "is not a valid integer." else: print "Verified string", my_string, "as a valid integer."