0

How do i write a function that accepts a tuple of numbers and return a tuple with odd numbers squared? Eg ((1,2,3)) becomes

(1,2,9)

12th Mar 2020, 4:51 PM
Tan
4 Respostas
+ 1
Hint: - Make a list of that tuple. You will work using the temporarily list (tuples are immutable). - Setup a for-loop to run within the list's length range - Within the loop, you check if element at a certain index was an odd number. If so, then you square it, otherwise leave them be. - Make a tuple of the modified list and return it to the caller. NOTE: Don't use for-each loop, it wouldn't work. Go and give it a try! 👍
12th Mar 2020, 5:47 PM
Ipang
+ 1
def func(t): return tuple([i if i%2==0 else i**2 for i in t]) print (func((1,2,3)))
12th Mar 2020, 6:10 PM
Qasem
0
How do i store the data after creating the looping and checking
13th Mar 2020, 5:33 AM
Tan
0
Def square_odd_terms(tpl) : For items in tpl: If items % 2! =0 Item * item Else Item
13th Mar 2020, 5:34 AM
Tan