# summing up all numbers from 0 to N, using a for loop import string #importing some useful string methods """ check_integer takes a string as input and: - returns True if the string is a valid representation of an integer - returns False otherwise """ def check_integer(my_string): # first check: the only legal characters are the minus sign and the digits. for character in my_string: if not(character in "-0123456789"): return False # second check: must have at least one digit. if (my_string == "") or (my_string == "-"): return False # third check: the minus sign is only allowed in the beginning. if ('-' in my_string[1:]): return False #if we did not find any violations, we have a valid integer. return True # start of main code # get N from the user, keep asking until an integer is entered while True: my_string = input("please enter N: ") if (check_integer(my_string) == False): print("string", my_string, "is not a valid integer") else: break N = int(my_string) # compute the sum total = 0 for i in range(0, N+1): total = total + i # print the result print("total =", total)