简体   繁体   中英

Print statement failing to output on terminal inside nested loops/function

This is my first question ever, so forgive me if I do something wrong.

I used Visual Studio to code a simple sorting algorithm this time, but I'm having a issue with it. I need to print the list of numbers not only to check if it is sorting, but also to see the final product. However, the terminal is not showing anything. Printing outside of the function, which has nested loops, works fine however. I tried to print a string as well, but nothing comes out. I assume I'm missing something simple, but I thought I would ask here since I've never asked a question here before. Thanks in advance. If there's any advice on asking questions, please don't hesitate to say.

bunny = 0
turtle = 0
temp = 0
sequence = [2, 8, 7, 1, 4]


def bubblesort():

    for x in sequence:
        turtle = sequence[x]
        for y in sequence:
            bunny = sequence[y]
            if sequence[y] < sequence[x]:
                temp = sequence[y]
                sequence[y] = sequence[x]
                sequence[x] = temp
                temp = 0
    return print(sequence)


print(sequence)

As @polortiz40 mentions in his comment you are not showing the call to bubblesort() in your snippet. However I am going to assume you do.

I think your code never reaches the return statement of the function, because it crashes before that.

for x in sequence:
        turtle = sequence[x]

x will be: 2, 8, 7,... At the second iteration you will be trying to access sequence[8] which does not exist.

Following on the comments saying the for loop won't iterate through indices, as expected, you can obtain the index through the "enumerate" method instead. I also made some refactoring: you don't need to declare bunny, turtle, temp at the beginning of the code, and I made it so bubblesort takes a vector as parameter instead of edit a global one.

sequence = [2, 8, 7, 1, 4]

def bubblesort(seq):
    for x, turtle in enumerate(seq):
        for y, bunny in enumerate(seq):
            if bunny < seq[x]:
                temp = bunny
                seq[y] = seq[x]
                seq[x] = temp
    print(seq)

bubblesort(sequence)

First of all 3 things

  • You didn't call the function bubblesort() then only the function will work.

  • return print(sequence) in a function will return None , because you are calling the print function to return and by default print function only return None and which will be returned from your function. You can change it to two lines as below

     print(sequence) return sequence
  • Your function has errors that you can see it after calling the function as in the below code.

     bunny = 0 turtle = 0 temp = 0 sequence = [2, 8, 7, 1, 4] def bubblesort(): for x in sequence: turtle = sequence[x] for y in sequence: bunny = sequence[y] if sequence[y] < sequence[x]: temp = sequence[y] sequence[y] = sequence[x] sequence[x] = temp temp = 0 print(sequence) return sequence print("After bubble sorting: ", bubblesort())

Instead of using the index value of list elements, you are using the value itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM