Help--nameerror problem for factorial


#1

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?


#2

you can use try and except:

def example(n):
    print(n)
try:
  print(example(x))
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


#3

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