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