2021年3月28日星期日

Replace the first occurrence of an element in the list with 100 if it is present in the list

Replace the first occurrence of an element in the list with 100 if it is present in the list.Here is my code:

def replace_list(my_list,k):  for count,item in enumerate(my_list):      if item==k:          print("found")          my_list[count]=100          break  print(my_list)                        k=int(input("Enter the number of elements"))  for n in range(0,k):  e=input()  my_list.append(e)  k=int(input("Enter the element to be removed"))  replace_list(my_list,k)  

It's not working if we accept the list from the user. Instead, if I pass the list as a predefined one it works.

replace_list([2,3,5],k)  
https://stackoverflow.com/questions/66848633/replace-the-first-occurrence-of-an-element-in-the-list-with-100-if-it-is-present March 29, 2021 at 12:06PM

没有评论:

发表评论