I've a text file that consists of names. I want the user to type any 3 names and add them to an empty list, For each name we'll tell the user whether they are nearby. But I am getting an error which starts from setting_near
.
user = input('Name any1 who is near to you ').split(',')
friends_open = open('friends.txt', 'r')
friends_read = friends_open.readlines()
friends_open.close()
near_by = []
near_by.append(user)
setting_near = set(near_by)
setting_friends = set(friends_read)
intersect = setting_near.intersection(setting_friends)
for n in intersect:
print(f'your {intersect} friend is here!! meet him ')
The problem is you're using append
, which will add the list user
to the list near_by
. append
doesn't concatenate, it adds to the list.
Change that to extend
, which does concatenate:
near_by.append(user)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.