I'm trying to make a program that calculates the sum of the first N odd numbers. where N is the number of the first odd numbers (e.g. N=4, then the first odds are 1,3,5,7)
it should output the odd numbers.
is the code correct? (given that it needs to be in a loop) or is there a better code for calculating the sum? Also, how to input validate negative numbers? I only know how to do it for integers. (the input needs to be only positive integers)
numberN=input("Please enter a positive number N: ")
sumofodd = 0
try:
digit = int(numberN)
if digit > 0:
for n in range (0,digit+1,1):
sumofodd = n*n
print("the sum of", digit, " is ", sumofodd)
except ValueError:
print("you did not enter a positive integer")
exit(1)
Thank you very much