dimanche 8 novembre 2020

Shrinking very large If statements in python

I have a situation where I have 4 variables named eax,ebx,ecx and edx, and any one of them may be set to any of these vars: var1,var,var3 and var4, however they are set as the string "varx" instead of the value of varx, but only one of them will every need fixing at a time. I have been using this if statement:

  if eax=="var1":
    eax=var1
  elif ebx=="var1":
    ebx=var1
  elif ecx=="var1":
    ecx=var1
  elif edx=="var1":
    edx=var1
  elif eax=="var2":
    eax=var2
  elif ebx=="var2":
    ebx=var2
  elif ecx=="var2":
    ecx=var2
  elif edx=="var2":
    edx=var2
  elif eax=="var3":
    eax=var3
  elif ebx=="var3":
    ebx=var3
  elif ecx=="var3":
    ecx=var3
  elif edx=="var3":
    edx=var3
  elif eax=="var4":
    eax=var4
  elif ebx=="var4":
    ebx=var4
  elif ecx=="var4":
    ecx=var4
  elif edx=="var4":
    edx=var4

But I am aware that this is very bad practise. Is there a way for me to compact this if statement?

Thanks.

Aucun commentaire:

Enregistrer un commentaire