Reputation: 39
I have a task to write a program which for a sequence number as a result give a geometric mean. I have to write this program without any package. The introduction of the program end, when users give a 0. The program should report an error if the first number is 0 and if the user gives a negative number.
def sequence():
prod=1
i=-1
x=1
n = int(input("Give a number: "))
while True:
if n == 0:
break
elif i==0:
print("Error")
else:
prod=prod*n
i=i+1
result=prod**(1/i)
print(" Średnia wartośc ciągu ", result)
return i, suma
sequence()
My program doesn't return anything.
Upvotes: 2
Views: 43
Reputation: 3029
I don't want to post the solution, I'd rather point out several problems with the code. Figuring them out on your own will make you a better coder. There are several problems with your algorithm:
i=0
elif i==0:
won't work as you expect it has to be an if i==0:
under if n==0:
, so that you exit the loop when you get 0
from the user and you print Error
when it is the first iteration. n = int(input("Give a number: "))
has to be within the loop, so that it gets assigned on every iteration of your algorithm. return i, suma
should be outside the while
loop. Upvotes: 1