I’m fairly new to python and for the life of me I can’t get this to work. I want to set this little script to check to see if what the user types in is equal to any of the names on the list and if it is execute a function. If what the user types in isn’t one of the names it should do a different function. This seems like it should be simple but I can’t figure it out. I’ve gotten it to work with multiple elif statements to check for each case but it seems like there should be a more elegant solution then simply typing out 50 elif statements every time I want to check for a name.
Current script:
names=['Scott', 'Doug', 'Sam', 'Harry']
typedname=str(input('What is your name?: '))
if typedname==['Scott' or 'Doug' or 'Sam' or 'Harry']:
print('you are '+typedname)
else:
print('You are not in the names list')
Python 3.2 brings an optmization for that cases:
will be translated to a
frozensetand the search will be in constant time and the set will be built while compiling bytecode.