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?
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])
+ 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.
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)
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...
0
Jayakrishna🇮🇳 I don't understand, so how can I adjust the coding?
0
Jayakrishna🇮🇳 it seems the k is useless...
0
Jayakrishna🇮🇳 btw thanks for your help
0
Joe Ma you're welcome..