What is wrong with this Nth from End code?

Advertisements

I can’t seem to figure out this code. I’ve written a program but seem to get value errors when executing.

My issue is that depending on the formatting of the input I get an error

I’m trying to read a series of positive integers from a list, then a negative integer.

When the input is like this:

1 2 3 4 5
-6

it shows me a value error

but when it’s input one at a time like this:

1
2
3
4
5
-6

I get the desired output.

Here is the commented code:

#Define a function find_nth_from_end, with parameters 'numbers' for positive numbers and 'n' representing the position fromt the end of the list
def find_nth_from_end(numbers,n):
    #Check if N is greater than the len of the list 'numbers'
    if n > len(numbers):
        #Return the negative of N
        return -n
    else:
        #If N is less than or equal to the range of the list, return the Nth number from end
        return numbers[-n]

#Store the main program in def main():
def main():
    #create an empty list to store positive numbers in 'numbers'
    numbers = []

    #Use a 'while' loop to receive the positive number inputs until a negative number is input
    while True:
        num = int(input())
        #Use an if statement to break the loop if num < 0
        if num < 0:
            #convert negative number to positive number to find N
            n = -num 
            break
        #Add positive number to list with .append
        numbers.append(num)

    #Find the Nth number from the end of the list
    result = find_nth_from_end(numbers, n)

    #Output result
    print(result)

#ensure program is run directly
if __name__ == "__main__":
    main()

I’ve tried rewrtiting the code several times,

here’s one example:

def main():
    numbers=[]

    while True:
        numbers_list=input()
        numbers_int_list= [int(num) for num in numbers_list.split()]
    
        numbers.append(numbers_int_list)
        neg_num= int(input())
        if neg_num<0:
            n = -neg_num
            break
        numbers.append(neg_num)

    result = find_nth_from_end(numbers, n)

    print(result)

Now if I input the data like this:

1 2 3 4 5
-6

It will output -6

but if I input new data

1 5 9 7 5
-3

I get the output of -3 when I am expecting to get 9 as the output.

>Solution :

For the first case, if you are entering 1 2 3 4 5 for the input, the variable is stored as a string "1 2 3 4 5" which can not be cast as a single int which throws the value error.

In your second case, you are doing slightly better by transforming the string ‘1 2 3 4 5’ into a list where each element is also converted from string to int, so now you have the proper [1, 2, 3, 4, 5] input. However, the append doesn’t work the way you think it does. It appends the whole list as a single element to the end of numbers, so numbers becomes [[1, 2, 3, 4, 5]] not [1, 2, 3, 4, 5], which only has one element! So naturally, asking for the 6th from the back gets -6. The same happens for your second input because it’s still only a list of 1 (list) element.

Leave a ReplyCancel reply