My code doesn't do the math when in a function definition but I dont know why. When i do it separate without the definitions it works fine but when calling an if with the function definitions it produces results as 0 or 1
#Write a program that asks the user for a number n and gives them the possibility to choose
# between computing the sum and computing the product of 1,…,n.
def summation(n):
adding = 0
while n != 0:
adding += n
n -= 1
print(adding)
def factorial(n):
product = 1
while n != 0:
product *= n
n -= 1
print(product)
n =0
num = int(input("Enter a number: "))
choice = int(input("Would you like Sum(1) or Product(2)"))
if choice == 1:
summation(n)
if choice == 2:
factorial(n)
There is only one mistake in your code . You are taking the number from user in 'num' variable & pass the 'n' into the summation & factorial function which has the value '0'.
Now you can run this code .
def summation(n):
adding = 0
while n != 0:
adding += n
n -= 1
print(adding)
def factorial(n):
product = 1
while n != 0:
product *= n
n -= 1
print(product)
n =0
num = int(input("Enter a number: "))
choice = int(input("Would you like Sum(1) or Product(2)"))
if choice == 1:
summation(num)
if choice == 2:
factorial(num)