I was practicing and I tried defining mathematical functions and other stuff, but then I got a bunch of errors and I ended up not having an error but the result was wrong. This is the code as of writing this:
def factorial(num):
n = 1
for i in range(1, num+1):
n *= i
return n
def series(start, end, expression):
m = 0
for n in range(start, end+1):
m+=eval(expression)
return m
ans1 = input("Do you want to use factorial or a series? ")
if ans1 == "Yes":
ans2 = input("Series or factorial? ")
if ans2 == "Series":
start = int(input("Select the starting value of n. "))
end = int(input("Select the final value of n. "))
expression = input("Type the expression to be run in terms of n. ")
m = series(start, end, expression)
print("The result is: " + str(m))
if ans2 == "Factorial":
factorizing = int(input("Select the number to factorize. "))
n = factorial(factorizing)
print(n)
elif ans1 == "No":
print("Ok!")
Thanks for the help, the factorial() is working perfectly now, but when in series i tried series(1, 5, 2*n+1), it gave me 35 when it was supposes to give 25, anyone know whats wrong?
You need to fix the logic. Such as start and end:
def factorial(num):
n = 1
for i in range(1, num + 1):
n *= i
return n
def series(start, end, expression):
m = 0
for n in range(start, end + 1):
m += eval(expression)
return m
ans1 = input("Do you want to use factorial or a series? Type `Yes` or `No`. ")
if ans1.lower() == "yes":
ans2 = input("Series or factorial? ")
if ans2.lower() == "series":
start_end = input("Type start and end values, like `3 4`: ")
start, end = start_end.split()
start, end = int(start), int(end)
if start > end:
start, end = end, start
expression = input("Type the expression to be run in terms of n: ")
result = series(start, end, expression)
print("The result is: " + str(result))
elif ans2.lower() == "factorial":
factorizing = int(input("Select the number to factorize: "))
result = factorial(factorizing)
print("The result is: " + str(result))
elif ans1.lower() == "no":
print("Ok!")
Do you want to use factorial or a series? Type `Yes` or `No`. yes
Series or factorial? series
Type start and end values, like `3 4`: 10 5
Type the expression to be run in terms of n: 4
The result is: 24
For Factorial:
Do you want to use factorial or a series? Type `Yes` or `No`. yes
Series or factorial? factorial
Select the number to factorize: 5
The result is: 120