""" A simple program, that gets an integer from the user, and prints the result of multiplying that integer by 2. If the user did not enter a valid integer, the system keeps asking. """ while True: text = input("please enter 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. # Now we can exit the while-loop and do whatever we want to do # with that integer. if (found_violation == False): break number = int(text) print(number * 2)