1

I'm currently pretty new to learning Python and have to complete an assignment which requires us to create a program that allows the user to do multiple things, one of them being that they can enter any amount of numbers into a list which is then bubble sorted. We aren't allowed to use in built sort functions or anything like that. I want to add an exception to my code/ specifically my function that appends inputted numbers to a list so that if the user enters any character other than an integer my program doesn't break but instead my own error message pops up. I usually know how to do this with a while loop and a try/exception, however I'm unsure of where and how to add this into my code as problems occur either way.

I've tried adding a while loop with the try/exception in different places throughout the function however either one of two things happen: my list just doesn't update. As the user enters numbers the program should print what numbers the user has entered and allow them to continue to enter numbers until they type 'sort', however when I add the try/exception when I enter a number nothing happens, or my program prints "no swaps required" and doesn't ask for any more numbers. The other thing that happens is that I get the following error: TypeError: object of type 'NoneType' has no len() referring to my BubbleSort() function which I have no idea about, it just goes over my head. Here's the function that appends inputted numbers to a list:

def AppendList():
    numberList = []
    while True:
        newNumbers = input("Please enter a number to add to the list. To begin "
                           "sorting your list, type 'sort'.\n ")
        if newNumbers == "sort":
            break
        numberList.append(int(newNumbers))
        print("This is how the list currently looks: " + str(numberList) +  ",")
    return numberList

TLDR; I want my program to allow the user to enter numbers to the list. Every time the user enters a number my program should say "This is how the list currently looks..." until they type 'sort' into the console. If the user enters a character that isn't a number I want my own error to pop up rather than the program breaking.

Here is a text file of the whole program itself: https://pastebin.com/wkwmK49Q

martineau
  • 119,623
  • 25
  • 170
  • 301
  • That's `try/except` not `try/exception`. Regardless, I suspect your question is a duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – martineau Feb 18 '19 at 21:47

3 Answers3

0

Wrap a try-except around the point where you convert it into an integer:

def AppendList():
    numberList = []
    while True:
        newNumbers = input("Please enter a number to add to the list. To begin sorting your list, type 'sort'.\n ")
        if newNumbers == "sort":
            break
        try:
            numberList.append(int(newNumbers))
        except ValueError:
            print("Your error message")
        print("This is how the list currently looks: " + str(numberList) +  ",")
    return numberList
iz_
  • 15,923
  • 3
  • 25
  • 40
0

You can wrap the portion of code that you want to be able to catch errors in:

try:
    input(...)
    [...]
except ValueError as e:
    print(e)

Or you can validate the string before converting it:

if not newNumbers.isdecimal():
    print('Not a valid number: ' + s)
jspcal
  • 50,847
  • 7
  • 72
  • 76
0

You can raise a ValueError with a Try-Except block, in this case the program will exit with an error message, but you can change the ValueError('') to a simple print function.

def AppendList():
    numberList = []
    while True:
        try:
            newNumbers = input("Please enter a number to add to the list. To begin sorting your list, type 'sort'.\n ")
            if newNumbers == "sort":
                break
            numberList.append(int(newNumbers))
        except ValueError: #Value error
            raise ValueError("ValueError")
        except: #Any other error
            print("any other error")
        print("This is how the list currently looks: " + str(numberList) +  ",")
    return numberList

Also You can add a function that checks the input, so no need for ValueError

newNumbers = str(input("Please enter a number to add to the list. To begin sorting your list, type 'sort'.\n "))
if(newNumber.isdigit() or newNumber == 'sort'): #Number or sort
    #ETC.
nagyl
  • 1,644
  • 1
  • 7
  • 18