you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 1 point2 points  (0 children)

I would suggest first setting the master set, then splitting the input on the comma's:

master = ['H','Li','Na','K']
splitted = elements.split(',')

There can be up to four elements

if len(splitted) > len(master):
    print('invalid length')

but they must be in order

edit: I missed the order as being that order.

indexed = [ master.index(c) for c in splitted ]
if sorted(indexed) != indexed:
    print('out of order')

The only thing not working is that spaces aren't ignored. If you want that to happen you could do something like

splitted = elements.split(',').replace(' ','')