An integer's(n's) power of 3 can be written as an addition of consecutive n odd numbers
+2

PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'''The programme takes integer inputs (say n) continuously and returns the n^3 as an addition of consecutive n odd numbers.The programme will terminate when the input is-1'''
while True:
num=input("Enter Input:")
a=num.find (".")
if a==-1:
try:
int_num=int (num)
if 1 <=int_num:
odd_list=range (1,int_num**3+1,2)
for i in range (len (odd_list)):
sum_list=odd_list [i:i+int_num]
if len (sum_list)==int_num and sum (sum_list)==int_num**3:
print ("Odd Number Sequence of {0}^3:".format(num),end=" ")
for i in range (len (sum_list)-1):
print ("{0} +".format(sum_list [i]),end=" ")
print (sum_list [len(sum_list)-1])
break
elif int_num==-1:
print ("Bye!")
break
else:
print ("Out of range!")
except ValueError:
print ("Enter an integer")
else:
print ("Enter an integer value")
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run