Skip to content
Advertisement

Practising python list and if

When i was practising list&if in python i got stuck with a problem

friends=["a","b","c"]
print("eklemek mi cikarmak mi istiyosunuz  ?")
ans=(input())
if ans == 'add':
  add=input("adding who  ?")
  friends.append(add)
if ans=='remove':
  remove = input("removing who ?")
  friends.remove(remove)
print(remove)

the code is above works fine but when i want to improve it with already existing friends and not having that friend i got stuck and having this error = if add in list: TypeError: argument of type ‘type’ is not iterable same goes to not having that friend to remove

friends=["a","b","c"]
print("add or remove ?  ?")
ans=(input())
if ans == 'add':
  add=input("adding who ? ?")
  if add in friends:
    print("you already added this person")
  else :
    friends.append(add)
if ans=='remove':
   remove = input("removing who ?")
   friends.remove(remove)
print(friends)

Advertisement

Answer

try this

friends=["a","b","c"]
print("add or remove ?  ?")
ans=(input())
if ans == 'add':
  add=input("adding who ? ?")
  if add in list:
    print("you already added this person")
  else :
    friends.append(add)
if ans=='remove':
   remove = input("removing who ?")
   if remove in friends: 
       friends.remove(remove)
   else:
        print('remove doesnot exist in list')
print(friends)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement