def ticket_type(items): for item in items[:]: if sum(item[0:3]) == sum(item[4:]): print('Lucky ticket') elif sum(item[0:3]) == sum(item[4:]-1): print('drunk ticket') items = '122134' ticket_type(items) |
1 answer
sumdoes not support string.- The cut says "take from the first to the second. Numbering from 0.
It turns out you need to convert the slice into numbers and take the correct slice in the second case:
def ticket_type(item): if type(item) is str and len(item) == 6: if sum(int(n) for n in item[0:3] if n.isdigit()) == sum(int(n) for n in item[3:] if n.isdigit()): print('Lucky ticket') else: print('drunk ticket') ticket_type('122134') # drunk ticket ticket_type('122221') # Lucky ticket |