python - Having trouble creating a function that takes one argument, and returns a result of adding odd numbers between 1 and argument? -


so far have this...

  def sumofodds(n):     result = 0     in range(1, n+1, 2):         result = result +         print(result) 

this gives me sum, prints numbers come before it. need sum, not rest of values.

you can use loop , range() 2 step

sum = 0 max = 11 in range(1, max, 2): # not including max     sum += print sum  #out:25 (1 + 3 + 5 + 7 + 9) 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -