Global name in Python

后端 未结 2 1905
别跟我提以往
别跟我提以往 2021-01-05 09:01

I want to find out whether two numbers N1 and N2 are the permutations of the same digits. For example 123 and 321

相关标签:
2条回答
  • 2021-01-05 09:27

    You need to define the arrays in the functions. And then append to it. arr1 = [] defines an empty array.

    arr1[k] = 2
    

    tries to change the value at index k. So either you should initialise it to a certain size or append to the empty array (using the append function).

    Also, if you want to access the arrays from outside the function, you might want to return the arrays from the function

    0 讨论(0)
  • 2021-01-05 09:52

    There is no need to do anything with global values here. Everything should be contained within the function.

    The problem is simply that you don't define arr1 or arr2 before you try appending to them. You need to define them in that function, along with s1, s2, k and fl.

    Edit I should add that your code is extremely unPythonic. All these while loops with incrementing counters should be replaced with for loops: for k in range(10) etc. But the first loop isn't even necessary - you should have arr1 = [0] * 10 and the same for arr2.

    0 讨论(0)
提交回复
热议问题