0

I want to use just one try\except for many variables. I want to find out which part is causing the error so I could print it to the user under the except part.

try:
    i = int(i)
    j = int(j)
    k = int(k)
    l = int(l)
    m = int(m)
    n = int(n)
    n+1 = int(n+1)
    ...
    ..
    .

except ValueError:

    print('Please enter an integer as {}'.format(# whether i, j, k, l, or ...))

I want to find out which int(x) is causing the error so I can print it to the user? Is there any way to do it? I don't want to try for each part because my variables are nearly infinite ;)

Ash
  • 263
  • 3
  • 14
  • Similar to [this question](https://stackoverflow.com/questions/14519177/python-exception-handling-line-number/20264059) – MyNameIsCaleb Sep 26 '19 at 19:51
  • 2
    Why do you have all those variables in the first place? Your example here would appear to make them more fitting to be used as a list. (And in turn your error would be able to be pinpointed via the iterable that populates the list) – Sayse Sep 26 '19 at 19:55
  • @Sayse, those are different tk entries, I want to know which is not int so I want to inform the user to input only integers! – Ash Sep 26 '19 at 19:57
  • @MyNameIsCaleb I have read your suggestion but it's not what I expressed. I want to know for which input (i,j,k,...) the user is entering letters instead of ints. But thanx anyway. – Ash Sep 26 '19 at 20:04

3 Answers3

2

How about something like this:

def parse_int(num, name='var'):
    try:
        return int(num)
    except ValueError:
        print(f"Please enter an integer for {name}")


i = parse_int(i, name='i')

You can call the parse_int method for other variables as you see fit.

PS: you can't assign to an expression like n+1 - that last line before the except in your code will not work

rdas
  • 20,604
  • 6
  • 33
  • 46
  • thank you. Yes I know `n+1` is wrong, I wrote it as extra clarification for what the problem is. Sorry but it's not exactly what I need. But thanx anyway. – Ash Sep 26 '19 at 20:02
  • This seems like the cleanest way to do it to me. Somehow you have to keep track of the object you are on. – MyNameIsCaleb Sep 26 '19 at 20:03
1

If all of them are integers, and if they are related in some way, then you can use a list to store them. The reason for another list with variable names is so you can use the same index to say which variable was error in.

my_list=[]
my_list_variables=["a","b","c"]
i=0
while True:
    try:
        x=int(input("Please input "+my_list_variables[i]+" : "))
        my_list.append(x)
        i=i+1
        #Just make sure to add a if statement to quit out of the loop
        #Like if that is end of the amount of input you are asking.
        break
    except ValueError:
        #index i doesn't change so you can tell the user which variable they
        #entered a wrong input for.
        print("Error: input "+my_list_variables[i]+" have to be a number")

Just have another list stored of name variables.

1

I think that @rdas has a better answer, but since you didn't like it, you can do something like this as well, it just looks a little hacky and not that much better than doing try/except on every line.

try:
    last = 'i'
    i = int('10')
    last = 'j'
    j = int('ten')
except ValueError:
    print(f'Please enter an integer as {last}')
MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
  • As I have said i,j,k,l,... are all tk entry values of a Biiiig form and I want to know where the user is trying to input anything than an integer. So I can print it back under the `except` part. Is there any way to know which i,j,k,l,... is raising the `ValueError`? Thanx +1. – Ash Sep 26 '19 at 20:11