简体   繁体   中英

My code is working but with no outputs, what is the problem?

What is the problem?

i even tried this at the starting soas to get a output

print("enter list elements")
arr = input()


def AlternateRearr(arr, n):

    arr.sort()

    v1 = list()
    v2 = list()

    for i in range(n):
        if (arr[i] % 2 == 0):
            v1.append(arr[i])

        else:
            v2.append(arr[i])

        index = 0
        i = 0
        j = 0
        Flag = False
 #set value to true is first element is even
        if (arr[0] % 2 == 0):
            Flag = True

#rearranging
        while(index < n):

            #if 1st elemnt is eevn
            if (Flag == True):
                arr[index] = v1[i]
                index += 1
                i+=1
                Flag = ~Flag

            else:
                arr[index] = v2[j]
                index +=1
                j += 1
                Flag = ~Flag


        for i in range(n):
            print(arr[i], end = "" )

            arr = [9, 8, 13, 2, 19, 14]
            n = len(arr)
            AlternateRearr(arr, n)
            print(AlternateRearr(arr))

There's no error. Just the driver code dosen't work i guess, there's no output.

no outputs

The only place where it could output anything is print(AlternateRearr(arr)) . But let's take a look at AlternateRearr itself - what does it return?

There's no return statement anywhere in AlternateRearr , so the print would show None. Well, it's something , not completely nothing...


But the code doesn't reach this part anyway - if it did, it would throw an error because print(AlternateRearr(arr)) passes only one argument to the function AlternateRearr that takes 2 arguments. You don't have default value set for n , so it wouldn't work.


Okay, so we came to conclusion that we don't reach the print anyway. But why ? Because you never call it. You only define it and it's a different thing from calling it.

You might encounter a problem if you just try calling it near your normal code - Python is an interpreted language, so your main-level code (not enclosed in functions) should be at the bottom of the file because it doesn't know anything that is below it.

Is that your complete code? Because you do have a function called AlternateRearr but you never call it

Call the function and also pass the integer for iteration. Add after the function:

AlternateRearr(arr, 5)

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