简体   繁体   中英

How to avoid repeating suits in if statements?

I have a code that looks:

if condition1:
  do1
  if condition2:
    do2
  else:
    do3
else:
  do3

where do2 and do3 operate on the same variables. Is there a way to reduce this code somehow, to avoid having do3 twice?

Thanks!

I would think of something like this to avoid do3 twice

if condition1:
    do1
elif condition2:
    do2
else:
    do3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM