How do i bypass a NameError name is not defined in python

Viewed 139

I have code that loops through checking users and storing the returned values in an array write them in a csv. At the moment I am getting an NameError because while it's looping the first time it's returning an empty value as there are no users to collect; I wanted to bypass this error. How can I assign a value to my array for I won't get the NameError, I tried adding the second and third line that made no difference:

            if len(df) == 0:
                df = "N/A"
            output = df.to_csv ( index=False,encoding = "utf-8")
            print(output)
1 Answers

You can add one if condition for empty value and raise a custom exception.

If (EmptyValueCondition):
   Raise InalidInputError

Try adding below custom Exception class and see.

class InalidInputError(Exception):
    pass
Related