为什么不能调用此函数
Why this Fuction cannot be called
我有这个列表,我想根据冒泡排序对它进行排序,代码中有一个函数 (Swap()) 拒绝工作。我不知道为什么。有代码
score = [92,95,7,5,85,55,789,47,125,3265,88,965,655,3,15,448,0,255,455]
size = len(score)
x = 0
COMPS = size - 1
def swap():
temp = score[x + 1]
score[x + 1] = score[x]
score[x] = temp
# The Sort Array Function
def SortArray():
y = 0
while y < COMPS:
x = 0
while x < COMPS:
if score[x] > score[x + 1]:
#This function not working.
swap()
x += 1
y += 1
#Display Array Function
def displayArray():
x = 0
while x < size:
print(score[x])
x += 1
SortArray()
displayArray()
但插入 swap() 代码,因此 swap( ) 并将其替换在 SortArray() 下方,在 if 条件 下方;就像这样:
def SortArray():
y = 0
while y < COMPS:
x = 0
while x < COMPS:
if score[x] > score[x + 1]:
#This Works
temp = score[x + 1]
score[x + 1] = score[x]
score[x] = temp
x += 1
y += 1
然后就可以了,所以我想知道为什么 swap() 函数在 SortArray() 下没有被调用
I want to know why the swap() function doesn't get called under the SortArray()
实际上,它被调用了——你可以自己检查一下,在或 using the step debugger 中添加几个 print()
调用——但它并没有按照你认为的那样做,因为你重新混淆局部变量和全局变量。
在SortArray()
中你定义了一个local变量命名为x
(它被定义为local因为你在函数中赋值),这显然是您希望 swap()
使用的那个。但是在你的 swap
函数中,你使用了一个变量 x
,它既不是函数的参数也不是函数内赋值的(两者都会使它成为局部变量),所以它被解析为 global x
上面声明。
IOW,swap
使用全局 x
为什么您希望它使用 SortArray()
本地的。这也是第二个版本有效的原因,因为这次它使用了正确的变量。
解决方案是删除全局 x
并将正确的值显式传递给 swap()
,即:
def swap(x):
temp = score[x + 1]
score[x + 1] = score[x]
score[x] = temp
def SortArray():
y = 0
while y < COMPS:
x = 0
while x < COMPS:
if score[x] > score[x + 1]:
swap(x)
x += 1
y += 1
当你这样做的时候,你也应该和 score
一样——实际上,你应该尽可能避免全局变量(相信我,你可以写一个 很多 代码而不使用全局变量):
def swap(score, x):
temp = score[x + 1]
score[x + 1] = score[x]
score[x] = temp
def SortArray(score):
comps = len(score) - 1
y = 0
while y < comps:
x = 0
while x < comps:
if score[x] > score[x + 1]:
swap(score, x)
x += 1
y += 1
def displayArray(score):
x = 0
while x < len(score):
print(score[x])
x += 1
if __name__ == "__main__":
score = [92,95,7,5,85,55,789,47,125,3265,88,965,655,3,15,448,0,255,455]
SortArray(score)
displayArray(score)
现在您的函数可以用于任何列表或序列。它们仍然完全不 pythonic 但这显然不是这里的重点(无论如何 python 内置了最优化的排序算法之一)。
我有这个列表,我想根据冒泡排序对它进行排序,代码中有一个函数 (Swap()) 拒绝工作。我不知道为什么。有代码
score = [92,95,7,5,85,55,789,47,125,3265,88,965,655,3,15,448,0,255,455]
size = len(score)
x = 0
COMPS = size - 1
def swap():
temp = score[x + 1]
score[x + 1] = score[x]
score[x] = temp
# The Sort Array Function
def SortArray():
y = 0
while y < COMPS:
x = 0
while x < COMPS:
if score[x] > score[x + 1]:
#This function not working.
swap()
x += 1
y += 1
#Display Array Function
def displayArray():
x = 0
while x < size:
print(score[x])
x += 1
SortArray()
displayArray()
但插入 swap() 代码,因此 swap( ) 并将其替换在 SortArray() 下方,在 if 条件 下方;就像这样:
def SortArray():
y = 0
while y < COMPS:
x = 0
while x < COMPS:
if score[x] > score[x + 1]:
#This Works
temp = score[x + 1]
score[x + 1] = score[x]
score[x] = temp
x += 1
y += 1
然后就可以了,所以我想知道为什么 swap() 函数在 SortArray() 下没有被调用
I want to know why the swap() function doesn't get called under the SortArray()
实际上,它被调用了——你可以自己检查一下,在或 using the step debugger 中添加几个 print()
调用——但它并没有按照你认为的那样做,因为你重新混淆局部变量和全局变量。
在SortArray()
中你定义了一个local变量命名为x
(它被定义为local因为你在函数中赋值),这显然是您希望 swap()
使用的那个。但是在你的 swap
函数中,你使用了一个变量 x
,它既不是函数的参数也不是函数内赋值的(两者都会使它成为局部变量),所以它被解析为 global x
上面声明。
IOW,swap
使用全局 x
为什么您希望它使用 SortArray()
本地的。这也是第二个版本有效的原因,因为这次它使用了正确的变量。
解决方案是删除全局 x
并将正确的值显式传递给 swap()
,即:
def swap(x):
temp = score[x + 1]
score[x + 1] = score[x]
score[x] = temp
def SortArray():
y = 0
while y < COMPS:
x = 0
while x < COMPS:
if score[x] > score[x + 1]:
swap(x)
x += 1
y += 1
当你这样做的时候,你也应该和 score
一样——实际上,你应该尽可能避免全局变量(相信我,你可以写一个 很多 代码而不使用全局变量):
def swap(score, x):
temp = score[x + 1]
score[x + 1] = score[x]
score[x] = temp
def SortArray(score):
comps = len(score) - 1
y = 0
while y < comps:
x = 0
while x < comps:
if score[x] > score[x + 1]:
swap(score, x)
x += 1
y += 1
def displayArray(score):
x = 0
while x < len(score):
print(score[x])
x += 1
if __name__ == "__main__":
score = [92,95,7,5,85,55,789,47,125,3265,88,965,655,3,15,448,0,255,455]
SortArray(score)
displayArray(score)
现在您的函数可以用于任何列表或序列。它们仍然完全不 pythonic 但这显然不是这里的重点(无论如何 python 内置了最优化的排序算法之一)。