I have a nickname list. It consists of two words. I have to make it so that if the user writes "Nick Vinokur", then the nick itself is thrown into the nick variable, and if he did not write anything after the word "nick", the program writes something to him. The fact is that I take the data from the user and make this code: nickname = text.split() . Next, I write: nick = nickname[1] . Everything is ready, but how to make the list checked for the presence of an index [1]? If it is not on the list, something will be displayed.
|
2 answers
if len(nickname) < 2: print('кое-что') |
You can check for an index in the list, for example, like this:
def index_check(index): nickname = ['nickname1', 'nickname2'] try: return nickname[int(index)] except IndexError as e: return e Check for a specific nickname in the list:
def name_check(name): nickname = ['nickname1', 'nickname2'] return name in nickname |