-2

So here's my code.

def bubblesort(list):
    global step
    global oldlist
    print("""
ORIGINAL LIST""")
    print(list)
    for i in range(len(list)):
        for j in range(len(list)-1):
             if float(list[j])>float(list[j+1]):
             list[j], list[j+1] = list[j+1], list[j]
    if oldlist==list:
    **end(list)**
    else:
        oldlist=list
        step=step+1
        print("""
STEP""",step)
        print(list)
    end(list)

def end(list):
    global step
    step=step+1
    print("""
STEP""",step)
    print(list)

step=0
oldlist=[]
list=[]
number=int(input("How many numbers do you want to input to sort? : "))
for i in range(1,number+1):
    value=float(input("INPUT NUMBER : "))
    list.append(value)
bubblesort(list)

The issue is the bit of code which I have is "end(list)" bare in mind I've included the ** to make it easier to see on here it's not actually in the code. It simply won't call the subroutine when I ask it to. I know it definitely runs through the if since if i put a "print" or "break" in they both work it just won't jump to a sub-routine. Furthermore, the time I call that subroutine later on in the code works and does go to the subroutine. So i'm a bit lost as to what I've done wrong here. Any help? Would be much appreciated :)

LewOF04
  • 1
  • 2
  • 1
    Aside from your other problems, don't use `list` as a variable name as you're overriding a Python keyword. – David Buck Jan 26 '22 at 11:31

1 Answers1

0

There are quite a few things wrong with your code, and your question. Let's go through them:

  1. First of all, the formatting was not great (I've suggested an edit). This might not sound like a big deal ("Hey, as long as it works...") but it's actually crucial: I work with python every day, and I had trouble understanding your code
  2. You're talking about subroutines. In python, we usually refer to those as functions, or methods.
  3. You're using globals variables all over the place. This might work in little toy examples like this one, but it will fall apart VERY fast
  4. As David Buck said, list is one of the basic data structures: 1 in an instance of int and [1,2,3] is an instance of list). It's a really bad idea to override it.
  5. I'm not sure what you're trying to do with the end() function. It seems do very little, and what it does is not related to its name...
  6. You create an oldlist list but never really do anything with it. What is it supposed to to ?

With that in mind, here is my proposition:

def main():
    # I like to put the main code in a "main" function, so that it can be at the top
    # of the file. We'll call main() from the bottom of the file

    # Make our program pretty, with a little branding
    print("===== Number Sorter 9000 =====")

    # 'numbers' is not the name of anything yet, so we can use it safely
    numbers = []

    # This will loop forever, unless we tell it to stop. It allows us to skip the
    # part where you ask the user for a number of values: Simply enter the values
    # one by one, and press enter once last time when done.
    while True:
        value = input(f"Number {len(numbers)+1} (leave empty to continue): ")

        # Decide what to do with the value we received. If it's a number,
        # add it to our list, if it's empty, stop collecting numbers
        if value:
            # Make sure the value is a number ()
            value = float(value)
            # Add the number to the list
            numbers.append(value)
        else:
            # This will get us out of the number-asking loop
            break

    print("Number Sorter 9000 will now sort your numbers")
    sorted_numbers = bubblesort(numbers)

    print("Number Sorter 9000 has sorted your numbers:")
    print(sorted_numbers)

def bubblesort(numbers):
    # Here I've removed things that were not doing much.
    # There are some ways to improve the algorithm itself, but I'll let you do that

    for i in range(len(numbers)):
        for j in range(0, len(numbers)-i-1):
            if numbers[j] > numbers[j+1]:
                numbers[j], numbers[j+1] = numbers[j+1], numbers[j]

    # We 'return' the result from the function, so that code that calls the
    # bubblesort() function can use the result
    return numbers

# Lastly, we call the "main" function to get everything started. Without this
# line, nothing happens: We've just explained to the computer how to do some
# stuff, but we haven't asked it to *DO* anything
main()

Well, I hope this was not too much information. I've tried to keep things simple, but feel free to ask if something is not clear

Dharman
  • 30,962
  • 25
  • 85
  • 135
bastien girschig
  • 663
  • 6
  • 26