0

I have 6 variables and trying to concatenate them. I am using try except method to handle the NameError.

Here is my code

try:
    var1 + var2 + var3 + var4 + var5 + var6

except NameError:
    var1 + var2 + var3 + var4 + var5 

I can handle first time NameError for var6 using except NameError

But How Can I handle more NameError for var5, and var4?

I am trying like this but got an error

try:
    var1 + var2 + var3 + var4 + var5 + var6

except NameError:
    var1 + var2 + var3 + var4 + var5 

except NameError:
    var1 + var2 + var3 + var4

except NameError:
    var1 + var2 + var3 

How can I handle multiple NameError?

Thanks

Raj
  • 69
  • 2
  • 10
  • This makes no sense - how do you not know which variables are defined? Unless you're doing ["variable variables"](https://stackoverflow.com/q/1373164/3001761), and you've shown exactly why that's a bad idea, or maybe you're defining them conditionally (also a bad idea). – jonrsharpe Jun 09 '22 at 07:54
  • @jonrsharpe, Is there any way to do this? – Raj Jun 09 '22 at 07:58
  • See https://meta.stackexchange.com/q/66377/248731 - there will be a solution, but to the X not the Y. – jonrsharpe Jun 09 '22 at 08:19

0 Answers0