""" This program: - asks the user to enter a string. - verifies that the string represents a positive 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 """ text = input("enter an integer: ") print("checking if", text, "is an integer...") text = text.strip() # found_violation will be set to True the moment we find a violation # of the integer format. found_violation = False # first check: the only legal characters are the minus sign and the digits. for character in text: if not(character in "-0123456789"): found_violation = True print("found non-digit character:", character); break # second check: must have at least one digit. if (text == "") or (text == "-"): found_violation = True print("This string does not contain any digits.") # third check: the minus sign is only allowed in the beginning. if ('-' in text[1:]): found_violation = True print("The minus sign is only allowed at the beginning.") #if we did not find any violations, we have a valid integer. if (found_violation == False): print("True") number = int(text)