0
Gentlemen,
I’m trying to solve the following exercise:
Exercise 2: Write a Python function, called soma_n, that takes an integer number, n, and computes the sum 1 + 2 + ... + n, if n > 0, or -1 + -2 + ... + -n, if n < 0. If n = 0, the sum is zero.
Your function should check whether the argument you receive is a number integer, through the isinstance(n, int) function, where n is the argument received. Otherwise the function should generate an error of the type Valueerror, through the raise statement Valueerror('soma_n: The limit must be an integer. '). For example,
soma_n(3) 6 soma_n(-3) -6 soma_n(0) 0 soma_n('anc') Traceback (Most recent call last): ... builtins.Valueerror: soma_n: The limit has to be an integer.
I don’t understand what should be done to adapt the isinstance(n, int) function to my code which is as follows:
def soma_n (n):
soma = 0
while (n != 0):
if n > 0:
soma = soma + n
n = n - 1
else:
soma = soma + n
n = n + 1
return (soma)
I do not understand this function isinstance, could help me how to use it next to my code?
Thanks in advance.