Computer Science, asked by shafeeqfsa, 2 months ago

Write a function to replace all the even numbers with its half and odd numbers with 1 from a

list.

Eg- if the original list is [12,23,11,45,90]

Modified list=[6,1,1,1,45]​

Answers

Answered by Tomboyish44
4

Co‎de: [Written using Python 3]

_______________________________

num_list = []

print()

print("This program will replace all the even numbers with its half and odd numbers with 1 from a given list.\n")

count = int(input("Enter the total no: of numbers you'd like to add to the list: "))

print()

for i in range(1, count + 1):

   num = float(input("Enter element number " + str(i) + ": "))

#Appending the value "num" to "num_list" using .append()

   num_list.append(num)  

print("\nList of numbers given:", num_list)

def odd_even():

   for element in num_list:

       if element == 0: #Added to prevent logical errors in the program

            break

       elif element % 2 == 0:

# Updating the value of the element to half of the original element in the list using list_name[index]

            num_list[num_list.index(element)] = element/2

       elif element % 2 != 0:

# Updating the value of the element to 1 in the list using list_name[index]

            num_list[num_list.index(element)] = 1

   print("\nModified list:", num_list)

odd_even() #Calling the function

______________________________

Output attached in the screenshot.

Attachments:
Similar questions