-4

I have recently been trying to code a number sorter. I searched the internet and decided to try out this piece of code:

numbers=([])
amount=input("How many numbers are in your list? ")
print("")
counter = 0
ran = 0
while counter < int(amount):
    counter = counter + 1
    ran = ran + 1
    num3 = input(str(ran) + ". Input: ")
    try:
        val = int(num3)
    except ValueError:
        num3 = input(str(ran) + ". Input: ")
    sort(numbers)
    numbers.append(num3)

At the beginning (when I try to run it), it seems to work; here are the first few lines of output:

How many numbers are in your list? (I input 7)

1. Input: (I input 3)

But then, this error appears right after I input 3 and press enter:

Traceback (most recent call last):
  File "D:/ZGMDCL8364/Documents/AllPyCharmProjects001/NumberSorter.py",   line 39, in <module>
    sort(numbers)
NameError: name 'sort' is not defined

And then I get this:

Process finished with exit code 1

How do I fix this? Please help.

Claire
  • 639
  • 9
  • 25
Zilgem8364
  • 11
  • 1

2 Answers2

1

Python has the sorted() built-in function, and sort() method for lists, used as list.sort().

There is not a sort() built-in function, and you have not defined one...

To define a function, say, sort()

def sort(any_arguments):
    # your code here
    return any_output_here

As @Matt Ball has commented about the documentation on sorting in Python, I would recommend going through Python tutorials to begin with.

Here's just an example: https://www.tutorialspoint.com/python/python_functions.htm

There are tonnes of books, videos and resources out there. MOOC online learning, such as Coursera, EdX, MIT, Standford, Udemy, etc, is often helpful. Interactive platforms such as Stepik are also fun options.

Quite often, error messages can help us debug by searching them to see if there are existing solutions. If not, it would be clearer to define the question by describing the situation and error met, instead of just stating there is "an error". These would help you climb the learning curve from getting in touch with a new language to tackling tasks or designing projects.

Happy coding~

Claire
  • 639
  • 9
  • 25
  • how do i define a sort() function? – Zilgem8364 May 12 '18 at 13:43
  • Edited answer to include how to define a function. Please also check out how to look for existing solutions and the practice of the community for Q&A. It will give you a nicer experience in the community and in programming in general ;) – Claire May 12 '18 at 14:00
0

As to your followup question of reading input, here's a simple approach for python 3:

num_count = input('Please enter the amount of numbers you wish to sort.\n')
numbers_input = input('Please enter a list of {} numbers.\n'.format(num_count))
numbers_list = [int(n) for n in numbers_input.split()]
print(sorted(numbers_list))

Line by line, first you ask the user for input for the number of numbers. Next, you ask the user for the numbers themselves. The input should be entered as several numbers on the same line separated by spaces. Next, you take this raw input, which is a string, and turn it into a list by splitting the string where there are spaces, and convert each element in the resulting list from a string to an integer. Finally, sort and print this list.

Note: regardless of how you implement your number sorter, you don't need the user to enter how many numbers are to be sorted. You can get this information with len(numbers_list).

Grant Palmer
  • 208
  • 2
  • 7