In: Computer Science
Python Programming, Below included the Skeleton, input and output. Could you also include indents in the solutions.
In this programming laboratory you will create a Python program that evaluates the sum of the following mathematical series:
Sum = x0 / 0! + x1 / 1! + x2 / 2! + x3 / 3! + … + xn / n!
The variable x is a real (float) number and the variable n is an integer that can assume values greater than or equal to 0. You program is to consist of a main program that allows the user to input values of x and n and then executes a loop with an index going from 1 to n. Your program should also include two functions, factorial and power, that calculate (respectively) factorial values and numbers to a power. The main program should call the factorial and power functions, from within the loop, to evaluate the expression for relevant values and add the value of the current term to the running sum. You may use the factorial function that was introduced in the examples in this week’s module (included in the skeleton of the program). You will need to write your own power function. The power function should compute x to the 0 power or x to positive integer powers. Do not worry about negative powers of x.
# Function to compute x to the k power
def power(x,k):
You will need to complete this function here
If x is 0 then return 0
Otherwise if n is 0 return 1
Otherwise use a loop to multiply x times itself n times and return the product
# The function below is the factorial function used in the Week Six examples
def factorial(num):
if num < 0:
print('Number must be >= 0')
exit()
else:
fac = 1
while num >= 1:
fac = fac * num
num = num - 1
return fac
# Main program
sum = 0.0
x = input("Enter x value ")
x = float(x)
n = input("Enter n value ")
n = int(n)
if n < 0:
print("n must be < 0")
quit()
…
You will need to complete your main
program here. It will
need to include a while loop that goes from 0 to n and calls to the
factorial and power functions.
After the loop, you will need to print the sum value
Your program should produce the results shown below when executed. Multiple executions with different x and n values are displayed.
Enter x value 2
Enter n value 0
Sum is 1.0
Enter x value 2
Enter n value 1
Sum is 3.0
Enter x value 2
Enter n value 2
Sum is 5.0
Enter x value 2
Enter n value 3
Sum is 6.333333333333333
*****
Following python code will take the value of x and n as input from user .Then the value of sum is calculated as
sum += power(x, n) / factorial(n)
where power method will return the value of x^n and factorial method will return the value of n!
*****
def power(x, n): ''' this method will compute x^n. If x is 0 it will return 0 , if n is 0 it will return 1 :param x: :param n: :return: ''' if x == 0: return 0 if n == 0: return 1 return x ** n def factorial(num): ''' this method will compute the factorial of a number :param num: :return: ''' if num < 0: return 0 else: fac = 1 while num >= 1: fac = fac * num num = num - 1 return fac if __name__ == '__main__': ''' this method is triggered when you run this python script. It will prompt the user to enter input values Then it will call power method and factorial method to compute the sum. Finally it will display the sum. ''' sum = 0.0 x = float(input('Enter x value')) n = int(input('Enter n value')) if n < 0: print("value of n must be greater than 0") print(f'sum = {sum}') exit() while n >= 0: sum += power(x, n) / factorial(n) n -= 1 print(f'sum = {sum}')
For any query or modification you require in the code , comment on my answer , i will be happy to resolve any of your query.