On assignement 1a, I tried to define a function, but kept getting a NameError that the function wasn't being recognized. Here is the function I tried to define: def IsThisNumberPrime(N): IsItPrime = True for Divisor in range(2, N-1): if N%Divisor == 0: IsItPrime = False break if IsItPrime == True: NumberOfPrimeNumbersFound += 1 ___________________________________________ I'm not sure why it caused an error. I took the function contents and put it directly in the main body of the code and it worked. Any ideas?
Without the exact wording of the error and the implementation of the function within the code it is difficult to diagnose. That said, the error you mention is generated when you reference a variable that doesn't exist: exception NameError Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found. The error should include the 'name' it was unable to find and it could be that when you made your call to the function, that you mistyped the function name, thus clearing the error when that call was erased from the code. From what I see here, your function looks correct. One sure way to test the situation is to remove the function from your main code, double check the spelling when you call it, and see if you can recreate the error.
You need to initialize NumberOfPrimeNumbersFound before you can increment it. But this counter belongs out in the calling code anyway, doesn't it?
Join our real-time social learning platform and learn together with your friends!