为什么我的bubble排序代码的continue循环外,以及我如何解决这个问题/为什么是bubble_sort(未定义)


`def bubble_sort(numbers):
# We set swapped to True so the loop looks runs at least once
swapped = True
while swapped:
swapped = False
for i in range(len(numbers) - 1):
if numbers[i] > numbers[i + 1]:
# Swap the elements
numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i]
# Set the flag to True so we'll loop again
swapped = True
results = bubble_sort(numbers)
UserInput = input("Please enter ten integer numbers with a space 
in between, or 'Quit' to exit: ")
numbers = UserInput.split()
print(UserInput)
while True:
if UserInput.lower() == 'quit':
break
if not UserInput.isdigit():
print("Invalid input.")
continue
else:
print(results)`

在我的代码中,bubble_sort(数字(得到一个错误,说明它是未定义的。这是什么原因?任何帮助都将不胜感激。

代码中有许多部分没有正确对齐。我给你举一个例子。

代码的这一部分没有对齐。

if not UserInput.isdigit():
print("Invalid input.")
continue #should be aligned to print statement
else: #not aligned. Should be aligned to if statement
print(results)

上述代码应如下所示:

if not UserInput.isdigit():
print("Invalid input.")
continue
else:
print(results)

请检查代码的所有部分,并确保它们对齐。

最新更新