我目前正在定义一个递归函数。它的目标是从最低到最高排序。当它开始时,我希望 A 等于 0,但我希望每次循环时该值都增加 1。这可能吗?这是到目前为止的功能。
def my_sort(number):
if a is None:
a=0
b=1
separated = number.split(',')
if separated[a]<= separated[b]:
while separated[0]<=separated[1]:
a = a+1
b = b+1
my_sort(number)
while separated[0]>separated[1]:
my_sort(number)
else:
new=0
separated[a] = new
separated[a].replace(separated[a],separated[b])
separated[b].replace(separated[b],new)
user_input= input("values to sort")
user_input= my_sort(user_input)
print user_input
请您参考如下方法:
更好的方法是将“a”和“b”作为参数传递给 my_sort()。那么您的初始调用是 my_sort(user_input, 0, 1) 并且您的递归调用是 my_sort(number, a+1, b+1)。
如果您不喜欢将 'a' 和 'b' 传递给 my_sort(),则定义使用 'a' 和 'b' 作为参数的 my_sort_helper() 并将 my_sort 实现为 my_short_helper (输入, 0, 1)
