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)
4 odpowiedzi
+ 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! 👍
+ 1
def func(t):
return tuple([i if i%2==0 else i**2 for i in t])
print (func((1,2,3)))
0
How do i store the data after creating the looping and checking
0
Def square_odd_terms(tpl) :
For items in tpl:
If items % 2! =0
Item * item
Else
Item