Help--nameerror problem for factorial


as shown on the figure, when I call the factorial function, by accident I entered a argument x, then I have the error. But if I enter 'x', the program works well.

My understanding is that, python takes x as a undefined variable instead of a string, but how to include this "accident" in my code so that my code can also work in the above case?


you can use try and except:

def example(n):
except NameError:
    print("undefined variable")

but then you would need to apply this on the function call, so far i know there is no other way, given the error already arises when you call the function


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.