Search code examples
algorithmredundancy

How to select duplicated code from if-statement?


I have the following algorithm:

if <some1>:
   h += 1
elif <some2>:
   h += 0.5
   i += 1
else:
   i += 1

Is it possible to do i += 1 just once?


Solution

  • I think the logic you want to express is:

    if <some1>:
       h += 1
    else:
       i += 1
       if <some2>:
          h+= 0.5
    

    Though it doesn't save you a whole lot.