Advertisement
OreganoHauch

Iterative Sum Function

Jan 11th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. # ITERATIVE SUM FUNCTION
  2.  
  3. n = int(input("Enter the number of addends in your sum: "))
  4.  
  5.  
  6. def sum_iterative(n):
  7. if n == 1:
  8. return "The answer is 1."
  9. elif n == 0:
  10. return "The answer is 0."
  11. elif n < 0:
  12. return "False argument"
  13. else:
  14. sum = 0
  15. for i in range(1, n + 1):
  16. sum += i
  17. return "The answer is " + str(sum) + "."
  18.  
  19.  
  20. print(sum_iterative(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement