c++ - "case sequence" in python implemented with dictionaries -
i have implemented function:
def postback(i,user,tval): """functie ce posteaza raspunsul bazei de date;stringul din mesaj tb sa fie mai mic de 140 de caractere""" result = { 1:api.postdirectmessage(user,'trebuie sa-mi spui si marca pe care o cauti'), 2:postmarket(user,tval), 3:api.postdirectmessage(user,'imi pare rau, dar nu stiu unde poti gasi aceste tipuri de smantana: %s' % tval)} return result.get(i)
but not work case alternative(from c++) executes 3 cases no matter wha try...i'm begginer there might error, please help!p.s. please don't tell me if...else.. alternative cause know can work
it executes 3 cases because define result
dict way! call 3 functions , assign them keys 1, 2, 3.
what should instead this:
functions = { 1: lambda: api.postdirectmessage(user,'trebuie sa-mi spui si marca pe care o cauti'), 2: lambda: postmarket(user,tval), 3: lambda: api.postdirectmessage(user,'imi pare rau, dar nu stiu unde poti gasi aceste tipuri de smantana: %s' % tval)} func = functions.get(i) if func: return func() else: raise valueerror("bad index: %d!" % i)
here define small wrapper functions , store in dict instead. pick right function , call on 1 function.
Comments
Post a Comment