Problem Definition :
Define functions:
• REPLACE(L,N) – this function takes a list L containing integers and an
integer N as parameters and increases the value of all odd elements by N and
decreases the value of all even elements by N.
• EXCHANGE(L) – this function takes a list L containing integers and
exchanges the first half elements of the list with the second half. ( Note : - if
the list has 6 elements then exchange first 3 elements with the last 3
elements and in case the list has 7 elements then exchange first 3 elements
with the last 3 elements leaving the middle element untouched.)
Both the functions will not display the list but only perform the task
Write a menu driven program in python which displays a menu to the user and
depending upon the user’s choice invokes the above mentioned functions and
displays the modified list.
PROGRAM CODE:
def replace(l,n):
for i in l:
if i%2==1:
i+=n
else:
i-=n
print(i)
def exchange(l):
mid=len(l)//2
for i in range(0,mid):
l[i],l[mid+i]= l[mid+i],l[i]
print(l)
, while True:
print("""MENU:
1.ADD ODD AND SUBTRACT EVEN
2.REPLACES FIRST HALF WITH SECOND HALF
3.EXIT""")
ch=int(input('enter choice'))
if ch==1:
l=eval(input("Enter elements of ure choice"))
n=int(input("Enter even or odd"))
replace(l,n)
elif ch==2:
l=eval(input("Enter elements of ure choice"))
exchange(l)
elif ch==3:
break
print()
O/P:
MENU:
1.ADD ODD AND SUBTRACT EVEN
2.REPLACES FIRST HALF WITH SECOND HALF
3.EXIT
enter choice1
Enter elements of ure choice[5,9,6,7,2,6,4]
Enter even or odd9
-5
MENU: