1

I have a python program where it prompts a user input for position or index and deletes the element in the list based on the position or index. The python program works but I'm having issues with the condition where if no user input is given, it automatically deletes the whole line in the list.

Example:

lst = [1,2,3,4,5]
enter position: 2

output: [1,2,4,5]

enter position: #user just pressed enter without giving any input

output: []

I'm writing the function within a class whereby:

def delete(self,index):
    """
    This function deletes an item based on the index
    :param self: the array
    :param index: the index of an item in the array
    :return: the array is updated
    :raises: IndexError if out of range
    """
    if not index:
        self.__init__()
    if index<0:
        index = index + self.count
    for i in range(index, self.count -1):
        self._array[i] = self._array[i+1]
    self.count-=1

and prompting the user input is as such:

position = int(input("Enter position:"))

it's not possible to just press 'enter' without receiving an error due to the position only receiving integers hence I'm looking for a method where if the user doesn't give any position, it registers it and prints just an empty list instead of an error message.

2
  • 3
    Why not just save the input string then convert it once you're sure that the input is a number? Commented Sep 17, 2017 at 2:17
  • position = input("Enter position:") then test if position.isdigit(): ... else: ... Commented Sep 17, 2017 at 2:19

1 Answer 1

1

What you're looking for is the try-except block. See the following for an example:

input_invalid = true
while input_invalid:
    user_input = input("Enter position: ")
    try:
        user_input = int(user_input)
        input_invalid = false
    except ValueError:
        print("Please enter a valid integer!")

Here, the try-except block catches any errors (of the type specified in except) thrown within the code block. In this case, the error results from trying to call int() on a string that does not contain an integer (ValueError). You can use this to explicitly prevent the error and control the logic flow of your program like shown above.

An alternate solution without using try-except is to use the .isdigit() method to validate the data beforehand. If you were to use .isdigit() (which I personally think is better), your code would look something like this:

input_invalid = true
while input_invalid:
    user_input = input("Enter position: ")
    if user_input.isdigit():
        input_invalid = false
    else:
        print("Please enter a valid integer!")

Hope this helped!

Sign up to request clarification or add additional context in comments.

1 Comment

With isdigit() you could also test if the input is in the list, and if not then use [] or ask the user for input again by not breaking out of the while loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.