Python 用户输入后排序数组不工作,并在 #主 def main(): #调用列表和变量 高尔夫球杆=[] 索引=0 分数=10 #输入分数10次 而(指数=1): 索引=0 而(索引golfScores[index+1]): 交换(高尔夫球杆[指数]、高尔夫球杆[指数+1]) 索引=索引+1 maxElement=maxElement-1 返回高尔夫球杆 #a&b作为参考值传递给交换 def交换(a、b): 温度=0 a=b b=温度 返回a,b #打电话给总经理让他跑 main()
每当我运行整个程序时,它接受输入1-10并显示良好,但在其他任何一个上,它似乎被卡住了,我必须中断键盘才能摆脱它。它似乎也会打印数组中的内容,但不会按顺序打印 所以我觉得泡泡运动不起作用。。我试着考虑过这个的交换布局,想知道它是否与此有关 努力完成 用户输入:3,2,1,4,6,5,8,7,10,9 输出:排序顺序为:[1,2,3,4,5,6,7,8,9,10]Python 用户输入后排序数组不工作,并在 #主 def main(): #调用列表和变量 高尔夫球杆=[] 索引=0 分数=10 #输入分数10次 而(指数=1): 索引=0 而(索引golfScores[index+1]): 交换(高尔夫球杆[指数]、高尔夫球杆[指数+1]) 索引=索引+1 maxElement=maxElement-1 返回高尔夫球杆 #a&b作为参考值传递给交换 def交换(a、b): 温度=0 a=b b=温度 返回a,b #打电话给总经理让他跑 main(),python,arrays,sorting,Python,Arrays,Sorting,每当我运行整个程序时,它接受输入1-10并显示良好,但在其他任何一个上,它似乎被卡住了,我必须中断键盘才能摆脱它。它似乎也会打印数组中的内容,但不会按顺序打印 所以我觉得泡泡运动不起作用。。我试着考虑过这个的交换布局,想知道它是否与此有关 努力完成 用户输入:3,2,1,4,6,5,8,7,10,9 输出:排序顺序为:[1,2,3,4,5,6,7,8,9,10] 问题实际上是在交换函数中。在Python中,int是按值传递的,因此swap函数不会影响bubbleSort函数中a和b的值 def
问题实际上是在交换函数中。在Python中,int是按值传递的,因此swap函数不会影响bubbleSort函数中a和b的值
def main():
# calling list and variables
SCORES = 10
golfScores = list(map(int, input().split(", ")))
if(len(golfScores) != SCORES):
return
# calling module to sort with 2 parameters passed
bubbleSort(golfScores, SCORES)
# print the final result
print('The sorted order is:', golfScores)
# going through the array to sort
def bubbleSort(golfScores, SCORES):
maxElement = SCORES - 1
while (maxElement >= 1):
index = 0
while (index <= maxElement - 1):
while (golfScores[index] > golfScores[index + 1]):
golfScores[index], golfScores[index + 1] = golfScores[index + 1], golfScores[index]
index = index + 1
maxElement = maxElement - 1
return golfScores
尝试删除交换功能,并替换您的行:
# the main
def main():
# calling list and variables
golfScores = []
index = 0
SCORES = 10
# enter scores 10 times
while (index <= SCORES - 1):
scoreInput = int(input('Enter score: '))
golfScores.append(scoreInput)
index = index + 1
# calling module to sort with 2 parameters passed
bubbleSort(golfScores, SCORES)
# print the final result
print('The sorted order is:', golfScores)
# going through the array to sort
def bubbleSort(golfScores, SCORES):
maxElement = SCORES - 1
while (maxElement >= 1):
index = 0
while (index <= maxElement - 1):
while (golfScores[index] > golfScores[index + 1]):
swap(golfScores[index], golfScores[index + 1])
index = index + 1
maxElement = maxElement - 1
return golfScores
# a & b passed as ref values to swap
def swap(a,b):
temp = 0
a = b
b = temp
return a,b
# call the main to run
main()
与:
这应该是:
while (golfScores[index] > golfScores[index + 1]):
swap(golfScores[index], golfScores[index + 1])
Python没有按引用传递。要交换两个变量,请使用
var1,var2=var2,var1
。您可以使用map
和split
来改进输入语句,例如golfScores=list(map(int,input().split(“,”))
,因为您当前正在将整行作为列表的一个元素读取
修订守则:
if (golfScores[index] > golfScores[index + 1]):
swap(golfScores[index], golfScores[index + 1])
def main():
#调用列表和变量
分数=10
golfScores=list(映射(int,input().split(“,”))
如果(len(高尔夫球杆)!=得分):
回来
#调用模块以使用传递的2个参数进行排序
泡泡运动(高尔夫球杆、分数)
#打印最终结果
打印('排序顺序为:',golfScores)
#通过数组进行排序
def bubbleSort(高尔夫球杆、得分):
maxElement=分数-1
而(maxElement>=1):
索引=0
而(索引golfScores[index+1]):
高尔夫球杆[指数],高尔夫球杆[指数+1]=高尔夫球杆[指数+1],高尔夫球杆[指数]
索引=索引+1
maxElement=maxElement-1
返回高尔夫球杆
我相信您遇到的问题是由于您的条件造成的,在某个点上导致了无限循环。我稍微修改了一段代码,请检查一下,如果有任何混淆,请询问。这样,就不需要使用swap函数了
def main():
# calling list and variables
SCORES = 10
golfScores = list(map(int, input().split(", ")))
if(len(golfScores) != SCORES):
return
# calling module to sort with 2 parameters passed
bubbleSort(golfScores, SCORES)
# print the final result
print('The sorted order is:', golfScores)
# going through the array to sort
def bubbleSort(golfScores, SCORES):
maxElement = SCORES - 1
while (maxElement >= 1):
index = 0
while (index <= maxElement - 1):
while (golfScores[index] > golfScores[index + 1]):
golfScores[index], golfScores[index + 1] = golfScores[index + 1], golfScores[index]
index = index + 1
maxElement = maxElement - 1
return golfScores
def main():
#调用列表和变量
高尔夫球杆=[]
索引=0
分数=10
#输入分数10次
而(指数=1):
索引=0
而(索引golfScores[index+1]:
温度=高尔夫球杆[指数]
高尔夫球杆[指数]=高尔夫球杆[指数+1]
高尔夫球杆[指数+1]=温度
指数+=1
maxElement=maxElement-1
返回高尔夫球杆
#a&b作为参考值传递给交换
def交换(a、b):
温度=0
a=b
b=温度
返回a,b
#打电话给总经理让他跑
main()
这些答案对你有用吗?
def main():
# calling list and variables
SCORES = 10
golfScores = list(map(int, input().split(", ")))
if(len(golfScores) != SCORES):
return
# calling module to sort with 2 parameters passed
bubbleSort(golfScores, SCORES)
# print the final result
print('The sorted order is:', golfScores)
# going through the array to sort
def bubbleSort(golfScores, SCORES):
maxElement = SCORES - 1
while (maxElement >= 1):
index = 0
while (index <= maxElement - 1):
while (golfScores[index] > golfScores[index + 1]):
golfScores[index], golfScores[index + 1] = golfScores[index + 1], golfScores[index]
index = index + 1
maxElement = maxElement - 1
return golfScores
def main():
# calling list and variables
golfScores = []
index = 0
SCORES = 10
# enter scores 10 times
while (index <= SCORES - 1):
scoreInput = int(input('Enter score: '))
golfScores.append(scoreInput)
index = index + 1
# calling module to sort with 2 parameters passed
golfScores = bubbleSort(golfScores, SCORES)
# print the final result
print('The sorted order is:', golfScores)
# going through the array to sort
def bubbleSort(golfScores, SCORES):
maxElement = SCORES
while (maxElement >= 1):
index = 0
while (index < maxElement - 1):
if golfScores[index] > golfScores[index + 1] :
temp = golfScores[index]
golfScores[index] = golfScores[index + 1]
golfScores[index + 1] = temp
index += 1
maxElement = maxElement - 1
return golfScores
# a & b passed as ref values to swap
def swap(a,b):
temp = 0
a = b
b = temp
return a,b
# call the main to run
main()