简体   繁体   English

我的代码正在运行,但是没有输出,这是什么问题?

[英]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)) . 唯一可以输出任何内容的地方是print(AlternateRearr(arr)) But let's take a look at AlternateRearr itself - what does it return? 但是,让我们看一下AlternateRearr本身-它返回什么?

There's no return statement anywhere in AlternateRearr , so the print would show None. AlternateRearr任何地方都没有return语句,因此print将显示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. 但是代码无论如何都不会到达这一部分-如果这样做的话,则会抛出错误,因为print(AlternateRearr(arr))仅将一个参数传递给具有2个参数的函数AlternateRearr You don't have default value set for n , so it wouldn't work. 您没有为n设置默认值,因此它将不起作用。


Okay, so we came to conclusion that we don't reach the print anyway. 好了,所以我们来到了结论,我们没有达到print反正。 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. 如果您只是尝试在常规代码附近调用它,则可能会遇到问题-Python是一种解释型语言,因此您的主代码(未包含在函数中)应位于文件的底部,因为它不知道在它下面。

Is that your complete code? 那是您完整的代码吗? Because you do have a function called AlternateRearr but you never call it 因为您确实有一个名为AlternateRearr的函数,但是您从未调用过它

Call the function and also pass the integer for iteration. 调用该函数,并将整数传递给迭代。 Add after the function: 在函数之后添加:

AlternateRearr(arr, 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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