When i was practising list&if in python i got stuck with a problem
JavaScript
x
11
11
1
friends=["a","b","c"]
2
print("eklemek mi cikarmak mi istiyosunuz ?")
3
ans=(input())
4
if ans == 'add':
5
add=input("adding who ?")
6
friends.append(add)
7
if ans=='remove':
8
remove = input("removing who ?")
9
friends.remove(remove)
10
print(remove)
11
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
JavaScript
1
14
14
1
friends=["a","b","c"]
2
print("add or remove ? ?")
3
ans=(input())
4
if ans == 'add':
5
add=input("adding who ? ?")
6
if add in friends:
7
print("you already added this person")
8
else :
9
friends.append(add)
10
if ans=='remove':
11
remove = input("removing who ?")
12
friends.remove(remove)
13
print(friends)
14
Advertisement
Answer
try this
JavaScript
1
17
17
1
friends=["a","b","c"]
2
print("add or remove ? ?")
3
ans=(input())
4
if ans == 'add':
5
add=input("adding who ? ?")
6
if add in list:
7
print("you already added this person")
8
else :
9
friends.append(add)
10
if ans=='remove':
11
remove = input("removing who ?")
12
if remove in friends:
13
friends.remove(remove)
14
else:
15
print('remove doesnot exist in list')
16
print(friends)
17