Develop a Python program that will calculate the sum of an integer series, as de
ID: 3563724 • Letter: D
Question
Develop a Python program that will calculate the sum of an integer series, as described below.
The program will consist of function squares, function cubes, and the main block of code.
Function squares has two parameters: the initial integer number in the series and the number of terms on the series. It will use repetition to compute the sum of the series, and then return the results. For example, if the first parameter is 3 and the second parameter is 4, the function will return 54.
Function cubes has two parameters: the initial integer number in the series and the number of terms on the series. It will use
repetition to compute the sum of the series, and then return the results. For example, if the first parameter is 3 and the
second parameter is 2, the function will return 91.
Sum = 33 +43 = 91
The program will repeatedly prompt the user to enter a command (a character string). The program will halt and display the
message
Explanation / Answer
# Function definition is here
def squares(start, num):
sum=0;
for i in range(0, num):
sum=sum+(start*start);
start=start+1;
return sum;
def cubes(start, num):
sum=0;
for i in range(0,num):
sum=sum+(start*start*start);
start=start+1;
return sum;
# Now you can call printme function
while True:
choice_exit=raw_input('Do You Want To Calculate y or n');
if(choice_exit=='y'):
choice=int(raw_input('Enter Your Choice(1. Square 2. Cubes)'));
if(choice==1):
initial=int(raw_input('Initial integer number in the series '));
term=int(raw_input('Number of terms in the series '));
square=squares(initial,term)
print("Squares of The Series is : ",square)
elif(choice==2):
initial=int(input('Initial integer number in the series '));
term=int(input('Number of terms in the series '));
cube=cubes(initial,term);
print("Cubes of The Series is : ",cube)
else:
print("***Invalid choice / Enter Valid Choice***");
else:
print('Program halted normally');
break;
Output
Do You Want To Calculate y or ny
Enter Your Choice(1. Square
2. Cubes)1
Initial integer number in the series 3
Number of terms in the series 4
Squares of The Series is : 86
Do You Want To Calculate y or ny
Enter Your Choice(1. Square
2. Cubes)2
Initial integer number in the series 3
Number of terms in the series 2
Cubes of The Series is : 91
Do You Want To Calculate y or nn
Program halted normally
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.