Python creating function

65 Views Asked by At

`Create a Python procedural function(procedure) named square_odd_numbers that takes a list of numbers as its argument. The procedure should print a new list containing the squares of only the odd numbers from the input list.

use a while loop in the answer.

Example:

Input: [1, 2, 3, 4, 5]

Output (printed to the console): [1, 9, 25]

Call the procedure using the above data as input.

For example: Test >>>>>>>>>>> Result

#only your code >>>>>> [1, 9, 25]

test_list = [111, 62, 35, 47, 5]>>>>>>>>[12321, 1225, 2209, 25] square_odd_numbers(test_list)

def square_odd_numbers():
    a = [1, 2, 3, 4, 5]
    i = 1
    while i <= 6:
    if i%2 != 0:
        print(i ** 2)
    i += 1
square_odd_numbers()

Test program is not accepting what I did. Can someone explain to me? It is python programming.`

1

There are 1 best solutions below

0
Jesse Sealand On

Solution:

Your function needs to accept a list as input. Your function needs to process the items in that list.

Here is an example of how that would work. You have the basic idea, though.

def square_odd_numbers(input_list):
    
    for value in input_list:
        
        if value%2 != 0:
            print(value ** 2)

test_list = [111, 62, 35, 47, 5]
square_odd_numbers(test_list)

# 12321
# 1225
# 2209
# 25