# summing up all numbers from 0 to N, using a for loop

import string #importing some useful string methods

# get N from the user, keep asking until an integer is entered
while True:
    my_string = input("please enter N: ")

    # this line removes whitespace from the beginning and the end.
    my_string = my_string.strip()
    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):
        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)
