Python Coding Challenge - get x

Here is my solution for the Python Coding Challenge for “Find the Xth number”
It is intentionally long to provide a program in a pseudocode-like format
As well as to satisfy all the conditions, starting with the base case

I’m finding the 'Test" results of limited value as there is not way to know what has or hasn’t passed.
For this challenge I passed 9 of the 10 tests.
So I don’t know yet what hasn’t passed
I will post an update when I find it.

def getX(x, nums): # write your code here #basecase if len(nums) <= 0: print("Empty list, no nth (x) value.") return nums #an empty list print("Here is the sorted list of numbers.") nums.sort() print(nums) print("There are " + str(len(nums)) + " numbers in the list") for i in nums[::]: if x > len(nums): print("Index {} is out of range, meaning x is a higher number than the length of the list.".format(x)) return 0 else: print("The number at index {} or the nth number (x) in the list is: ".format(x -1)) return nums[x-1] #Tests print(getX(2, [6, 3, -1, 5]),"\n") print(getX(4, [6, 3, -1, 5, 99, 2]),"\n") print(getX(4, []),"\n") print(getX(5, [200, 9, 7, 35,]))

I did that. If x is out of range, or a higher index than the length of the list it then “x” cannot be found.

this is the function call to test it:

print(getX(5, [200, 9, 7, 35,]))

Since there are four elements (numbers) in the list, the Xth or 5th (index) does not exist so can’t be found or return

In that case it will return 0 which is in the project instructions.

but why is the if-else stuff in a loop?

Also, maybe you need to return 0 if x is negative.