0

Change some of the numbers on list by function

scores = [90,75,55,80,57,60] k = [] def func (list): for i in range(len(list)): if list[i] >=55 and list[i] <= 59: list[i] = 60 return(k.append(list[i])) func (scores) Here's a list and function, I want to change the number on list which below 59 and above 55 to 60, but my coding fail, why?

31st Oct 2020, 3:42 PM
Joe Ma
Joe Ma - avatar
8 Answers
+ 11
Just to show what is possible with a comprehension. It does only print the values. If a new list with values is required, a small modification is needed: scores = [90,75,55,80,57,60] print(scores) # test only print([60 if 55 <= num <= 59 else num for num in scores])
31st Oct 2020, 4:41 PM
Lothar
Lothar - avatar
+ 2
#Ok check this code changes... scores = [90,75,55,80,57,60] k = [] def func (list): for i in range(len(list)): if list[i] >=55 and list[i] <= 59: list[i] = 60 return list #returning list after loop finally... print(func (scores)) #printing returned value.. . #Edit : Joe Ma if you don't want to change original list, you can use another list k.
31st Oct 2020, 3:57 PM
Jayakrishna 🇮🇳
0
below is another try, also fail... scores = [90,75,55,80,57,60] k = [] def func (list): for i in scores: if i >=55 and i <= 59: list[i] = 60 return(k.append(i)) func (scores)
31st Oct 2020, 3:45 PM
Joe Ma
Joe Ma - avatar
0
I is index in 1st.and in 2nd it's value then k.append(I) will give you wrong and return is in for loop so on first iteration it return from function.... Joe Ma Finally, you are not printing actually returned value...
31st Oct 2020, 3:51 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 I don't understand, so how can I adjust the coding?
31st Oct 2020, 3:56 PM
Joe Ma
Joe Ma - avatar
0
Jayakrishna🇮🇳 it seems the k is useless...
31st Oct 2020, 4:02 PM
Joe Ma
Joe Ma - avatar
0
Jayakrishna🇮🇳 btw thanks for your help
31st Oct 2020, 4:03 PM
Joe Ma
Joe Ma - avatar
0
Joe Ma you're welcome..
31st Oct 2020, 4:07 PM
Jayakrishna 🇮🇳