Python问题:无法访问程序中的选项
当我在菜单中选择选项 4 时,我不断收到 TypeError: Cannot unpack non-iterable int object。我该如何解决这个问题,我已经尝试了几个小时但没有成功。代码如下:
from turtle import right
def selection_sort(altered_number_list, sorting_order):
if sorting_order == 1:
for value in range (0, len(altered_number_list)-1):
least_value = value
for index in range (value+1, len(altered_number_list)):
if altered_number_list[index] < altered_number_list[least_value]:
altered_number_list[index], altered_number_list[least_value] = altered_number_list[least_value], altered_number_list[index]
return altered_number_list
else:
for value in range (0, len(altered_number_list)-1):
least_value = value
for index in range (value+1, len(altered_number_list)):
if altered_number_list[index] > altered_number_list[least_value]:
altered_number_list[index], altered_number_list[least_value] = altered_number_list[least_value], altered_number_list[index]
return altered_number_list
def bubble_sort(altered_number_list, sorting_order):
if sorting_order == 1:
for value in range (len(altered_number_list)):
for index in range (0, len(altered_number_list)-1):
if altered_number_list[index] > altered_number_list[index+1]:
altered_number_list[index], altered_number_list[index+1] = altered_number_list[index+1], altered_number_list[index]
return altered_number_list
else:
for value in range (len(altered_number_list)):
for index in range (0, len(altered_number_list)-1):
if altered_number_list[index] < altered_number_list[index+1]:
altered_number_list[index], altered_number_list[index+1] = altered_number_list[index+1], altered_number_list[index]
return altered_number_list
def insertion_sort(altered_number_list, sorting_order):
if sorting_order == 1:
for value in range(1, len(altered_number_list)):
insert_value = number_list[value]
data_switch = value
while data_switch > 0 and altered_number_list[data_switch-1] > insert_value:
altered_number_list[data_switch] = altered_number_list[data_switch-1]
data_switch -= 1
altered_number_list[data_switch] = insert_value
return altered_number_list
else:
for value in range(1, len(altered_number_list)):
insert_value = number_list[value]
data_switch = value
while data_switch > 0 and altered_number_list[data_switch-1] < insert_value:
altered_number_list[data_switch] = altered_number_list[data_switch-1]
data_switch -= 1
altered_number_list[data_switch] = insert_value
return altered_number_list
def quick_sort(altered_number_list, number_list_min, number_list_max, sorting_order):
if number_list_min < number_list_max:
split_value = partition_sort(altered_number_list, number_list_min, number_list_max, sorting_order)
quick_sort(altered_number_list, number_list_min, split_value-1, sorting_order)
quick_sort(altered_number_list, split_value+1, number_list_max, sorting_order)
def partition_sort(alter_number_list, number_list_min, number_list_max, sorting_order):
if sorting_order == 1:
pivot_value = alter_number_list[number_list_min]
left_value = number_list_min + 1
right_value = number_list_max
complete = False
while not complete:
while left_value <= right_value and alter_number_list[left_value] <= pivot_value:
left_value = left_value + 1
while alter_number_list[right_value] >= pivot_value and right_value >= left_value:
right_value = right_value - 1
if right_value < left_value:
complete = True
else:
alter_number_list[left_value], alter_number_list[right_value] = alter_number_list[right_value], alter_number_list[left_value]
alter_number_list[number_list_min], alter_number_list[right_value] = alter_number_list[right_value] = alter_number_list[number_list_min]
return alter_number_list
else:
pivot_value = alter_number_list[number_list_min]
left_value = number_list_min + 1
right_value = number_list_max
complete = False
while not complete:
while left_value <= right_value and alter_number_list[left_value] >= pivot_value:
left_value = left_value + 1
while alter_number_list[right_value] <= pivot_value and right_value >= left_value:
right_value = right_value - 1
if right_value < left_value:
complete = True
else:
alter_number_list[left_value], alter_number_list[right_value] = alter_number_list[right_value], alter_number_list[left_value]
alter_number_list[number_list_min], alter_number_list[right_value] = alter_number_list[right_value] = alter_number_list[number_list_min]
return alter_number_list
import random
print("This program will sort a list of random numbers using a user determined sorting algorithm.")
sorting_algorithms = ["Selection Sort", "Bubble Sort", "Insertion Sort", "Quick Sort"]
sorting_orders = ["Ascending", "Descending"]
number_list = []
while True:
try:
random_numbers = int(input("Input number of random integers generated: "))
print("Sorting Algorithms: ")
for sort in range (0, len(sorting_algorithms)):
print(f"{sort+1} - {sorting_algorithms[sort]}")
sorting_algorithm = int(input("Select sorting algorithm: "))
print("Sorting Orders: ")
for sort in range (0, len(sorting_orders)):
print(f"{sort+1} - {sorting_orders[sort]}")
sorting_order = int(input("Select sorting algorithm order: "))
if random_numbers < 1 or random_numbers > 999999:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to 999999 are accepted.")
continue
elif sorting_algorithm < 1 or sorting_algorithm > 4:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to four are accepted.")
continue
elif sorting_order < 1 or sorting_order > 2:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to two are accepted.")
continue
for i in range(random_numbers):
number_list.append(random.randint(0, 999999))
print (number_list)
number_list_min = 0
number_list_max = len(number_list) - 1
if sorting_algorithm == 1:
altered_number_list = selection_sort(number_list, sorting_order)
break
elif sorting_algorithm == 2:
altered_number_list = bubble_sort(number_list, sorting_order)
break
elif sorting_algorithm == 3:
altered_number_list = insertion_sort(number_list, sorting_order)
break
elif sorting_algorithm == 4:
altered_number_list = quick_sort(number_list, number_list_min, number_list_max, sorting_order)
break
except:
print("Invalid input, integer values in the program specified parameters are accepted.")
print(altered_number_list)
在上面的代码中,我可以毫无困难地访问选项 1、2 和 3,但我无法访问第四个选项,因为它显示错误消息。此时,我已尝试重命名变量,使用不同的循环和操作,并创建单独的函数,但我仍然无法访问第四个选项,即快速排序算法。非常感谢任何帮助!
这是快速排序和分区排序的代码:
def quick_sort(altered_number_list, number_list_min, number_list_max, sorting_order):
if number_list_min < number_list_max:
split_value = partition_sort(altered_number_list, number_list_min, number_list_max, sorting_order)
quick_sort(altered_number_list, number_list_min, split_value-1, sorting_order)
quick_sort(altered_number_list, split_value+1, number_list_max, sorting_order)
def partition_sort(alter_number_list, number_list_min, number_list_max, sorting_order):
if sorting_order == 1:
pivot_value = alter_number_list[number_list_min]
left_value = number_list_min + 1
right_value = number_list_max
complete = False
while not complete:
while left_value <= right_value and alter_number_list[left_value] <= pivot_value:
left_value = left_value + 1
while alter_number_list[right_value] >= pivot_value and right_value >= left_value:
right_value = right_value - 1
if right_value < left_value:
complete = True
else:
alter_number_list[left_value], alter_number_list[right_value] = alter_number_list[right_value], alter_number_list[left_value]
alter_number_list[number_list_min], alter_number_list[right_value] = alter_number_list[right_value] = alter_number_list[number_list_min]
return alter_number_list
else:
pivot_value = alter_number_list[number_list_min]
left_value = number_list_min + 1
right_value = number_list_max
complete = False
while not complete:
while left_value <= right_value and alter_number_list[left_value] >= pivot_value:
left_value = left_value + 1
while alter_number_list[right_value] <= pivot_value and right_value >= left_value:
right_value = right_value - 1
if right_value < left_value:
complete = True
else:
alter_number_list[left_value], alter_number_list[right_value] = alter_number_list[right_value], alter_number_list[left_value]
alter_number_list[number_list_min], alter_number_list[right_value] = alter_number_list[right_value] = alter_number_list[number_list_min]
return alter_number_list
我认为主要问题在于以下代码:
while True:
try:
random_numbers = int(input("Input number of random integers generated: "))
print("Sorting Algorithms: ")
for sort in range (0, len(sorting_algorithms)):
print(f"{sort+1} - {sorting_algorithms[sort]}")
sorting_algorithm = int(input("Select sorting algorithm: "))
print("Sorting Orders: ")
for sort in range (0, len(sorting_orders)):
print(f"{sort+1} - {sorting_orders[sort]}")
sorting_order = int(input("Select sorting algorithm order: "))
if random_numbers < 1 or random_numbers > 999999:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to 999999 are accepted.")
continue
elif sorting_algorithm < 1 or sorting_algorithm > 4:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to four are accepted.")
continue
elif sorting_order < 1 or sorting_order > 2:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to two are accepted.")
continue
for i in range(random_numbers):
number_list.append(random.randint(0, 999999))
print (number_list)
number_list_min = 0
number_list_max = len(number_list) - 1
if sorting_algorithm == 1:
altered_number_list = selection_sort(number_list, sorting_order)
break
elif sorting_algorithm == 2:
altered_number_list = bubble_sort(number_list, sorting_order)
break
elif sorting_algorithm == 3:
altered_number_list = insertion_sort(number_list, sorting_order)
break
elif sorting_algorithm == 4:
altered_number_list = quick_sort(number_list, number_list_min, number_list_max, sorting_order)
break
except:
print("Invalid input, integer values in the program specified parameters are accepted.")
I keep recieving an TypeError: cannot unpack non-iterable int object when I select option 4 in the menu. How do I fix this, I have tried for hours with no success. Code is below:
from turtle import right
def selection_sort(altered_number_list, sorting_order):
if sorting_order == 1:
for value in range (0, len(altered_number_list)-1):
least_value = value
for index in range (value+1, len(altered_number_list)):
if altered_number_list[index] < altered_number_list[least_value]:
altered_number_list[index], altered_number_list[least_value] = altered_number_list[least_value], altered_number_list[index]
return altered_number_list
else:
for value in range (0, len(altered_number_list)-1):
least_value = value
for index in range (value+1, len(altered_number_list)):
if altered_number_list[index] > altered_number_list[least_value]:
altered_number_list[index], altered_number_list[least_value] = altered_number_list[least_value], altered_number_list[index]
return altered_number_list
def bubble_sort(altered_number_list, sorting_order):
if sorting_order == 1:
for value in range (len(altered_number_list)):
for index in range (0, len(altered_number_list)-1):
if altered_number_list[index] > altered_number_list[index+1]:
altered_number_list[index], altered_number_list[index+1] = altered_number_list[index+1], altered_number_list[index]
return altered_number_list
else:
for value in range (len(altered_number_list)):
for index in range (0, len(altered_number_list)-1):
if altered_number_list[index] < altered_number_list[index+1]:
altered_number_list[index], altered_number_list[index+1] = altered_number_list[index+1], altered_number_list[index]
return altered_number_list
def insertion_sort(altered_number_list, sorting_order):
if sorting_order == 1:
for value in range(1, len(altered_number_list)):
insert_value = number_list[value]
data_switch = value
while data_switch > 0 and altered_number_list[data_switch-1] > insert_value:
altered_number_list[data_switch] = altered_number_list[data_switch-1]
data_switch -= 1
altered_number_list[data_switch] = insert_value
return altered_number_list
else:
for value in range(1, len(altered_number_list)):
insert_value = number_list[value]
data_switch = value
while data_switch > 0 and altered_number_list[data_switch-1] < insert_value:
altered_number_list[data_switch] = altered_number_list[data_switch-1]
data_switch -= 1
altered_number_list[data_switch] = insert_value
return altered_number_list
def quick_sort(altered_number_list, number_list_min, number_list_max, sorting_order):
if number_list_min < number_list_max:
split_value = partition_sort(altered_number_list, number_list_min, number_list_max, sorting_order)
quick_sort(altered_number_list, number_list_min, split_value-1, sorting_order)
quick_sort(altered_number_list, split_value+1, number_list_max, sorting_order)
def partition_sort(alter_number_list, number_list_min, number_list_max, sorting_order):
if sorting_order == 1:
pivot_value = alter_number_list[number_list_min]
left_value = number_list_min + 1
right_value = number_list_max
complete = False
while not complete:
while left_value <= right_value and alter_number_list[left_value] <= pivot_value:
left_value = left_value + 1
while alter_number_list[right_value] >= pivot_value and right_value >= left_value:
right_value = right_value - 1
if right_value < left_value:
complete = True
else:
alter_number_list[left_value], alter_number_list[right_value] = alter_number_list[right_value], alter_number_list[left_value]
alter_number_list[number_list_min], alter_number_list[right_value] = alter_number_list[right_value] = alter_number_list[number_list_min]
return alter_number_list
else:
pivot_value = alter_number_list[number_list_min]
left_value = number_list_min + 1
right_value = number_list_max
complete = False
while not complete:
while left_value <= right_value and alter_number_list[left_value] >= pivot_value:
left_value = left_value + 1
while alter_number_list[right_value] <= pivot_value and right_value >= left_value:
right_value = right_value - 1
if right_value < left_value:
complete = True
else:
alter_number_list[left_value], alter_number_list[right_value] = alter_number_list[right_value], alter_number_list[left_value]
alter_number_list[number_list_min], alter_number_list[right_value] = alter_number_list[right_value] = alter_number_list[number_list_min]
return alter_number_list
import random
print("This program will sort a list of random numbers using a user determined sorting algorithm.")
sorting_algorithms = ["Selection Sort", "Bubble Sort", "Insertion Sort", "Quick Sort"]
sorting_orders = ["Ascending", "Descending"]
number_list = []
while True:
try:
random_numbers = int(input("Input number of random integers generated: "))
print("Sorting Algorithms: ")
for sort in range (0, len(sorting_algorithms)):
print(f"{sort+1} - {sorting_algorithms[sort]}")
sorting_algorithm = int(input("Select sorting algorithm: "))
print("Sorting Orders: ")
for sort in range (0, len(sorting_orders)):
print(f"{sort+1} - {sorting_orders[sort]}")
sorting_order = int(input("Select sorting algorithm order: "))
if random_numbers < 1 or random_numbers > 999999:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to 999999 are accepted.")
continue
elif sorting_algorithm < 1 or sorting_algorithm > 4:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to four are accepted.")
continue
elif sorting_order < 1 or sorting_order > 2:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to two are accepted.")
continue
for i in range(random_numbers):
number_list.append(random.randint(0, 999999))
print (number_list)
number_list_min = 0
number_list_max = len(number_list) - 1
if sorting_algorithm == 1:
altered_number_list = selection_sort(number_list, sorting_order)
break
elif sorting_algorithm == 2:
altered_number_list = bubble_sort(number_list, sorting_order)
break
elif sorting_algorithm == 3:
altered_number_list = insertion_sort(number_list, sorting_order)
break
elif sorting_algorithm == 4:
altered_number_list = quick_sort(number_list, number_list_min, number_list_max, sorting_order)
break
except:
print("Invalid input, integer values in the program specified parameters are accepted.")
print(altered_number_list)
In the code above, I am able to access options 1, 2, and 3 with no difficulty but I am unable to access the 4th option as it displays an error message. At this point, I have tried renaming variables, using different loops and operations, and creating separate functions but I am still unable to access the 4th option which is the quick sort algorithm. Any help is greatly appreciated!
Here is the code for the quick sort and partition sort:
def quick_sort(altered_number_list, number_list_min, number_list_max, sorting_order):
if number_list_min < number_list_max:
split_value = partition_sort(altered_number_list, number_list_min, number_list_max, sorting_order)
quick_sort(altered_number_list, number_list_min, split_value-1, sorting_order)
quick_sort(altered_number_list, split_value+1, number_list_max, sorting_order)
def partition_sort(alter_number_list, number_list_min, number_list_max, sorting_order):
if sorting_order == 1:
pivot_value = alter_number_list[number_list_min]
left_value = number_list_min + 1
right_value = number_list_max
complete = False
while not complete:
while left_value <= right_value and alter_number_list[left_value] <= pivot_value:
left_value = left_value + 1
while alter_number_list[right_value] >= pivot_value and right_value >= left_value:
right_value = right_value - 1
if right_value < left_value:
complete = True
else:
alter_number_list[left_value], alter_number_list[right_value] = alter_number_list[right_value], alter_number_list[left_value]
alter_number_list[number_list_min], alter_number_list[right_value] = alter_number_list[right_value] = alter_number_list[number_list_min]
return alter_number_list
else:
pivot_value = alter_number_list[number_list_min]
left_value = number_list_min + 1
right_value = number_list_max
complete = False
while not complete:
while left_value <= right_value and alter_number_list[left_value] >= pivot_value:
left_value = left_value + 1
while alter_number_list[right_value] <= pivot_value and right_value >= left_value:
right_value = right_value - 1
if right_value < left_value:
complete = True
else:
alter_number_list[left_value], alter_number_list[right_value] = alter_number_list[right_value], alter_number_list[left_value]
alter_number_list[number_list_min], alter_number_list[right_value] = alter_number_list[right_value] = alter_number_list[number_list_min]
return alter_number_list
I believe the main issue lies in the following code:
while True:
try:
random_numbers = int(input("Input number of random integers generated: "))
print("Sorting Algorithms: ")
for sort in range (0, len(sorting_algorithms)):
print(f"{sort+1} - {sorting_algorithms[sort]}")
sorting_algorithm = int(input("Select sorting algorithm: "))
print("Sorting Orders: ")
for sort in range (0, len(sorting_orders)):
print(f"{sort+1} - {sorting_orders[sort]}")
sorting_order = int(input("Select sorting algorithm order: "))
if random_numbers < 1 or random_numbers > 999999:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to 999999 are accepted.")
continue
elif sorting_algorithm < 1 or sorting_algorithm > 4:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to four are accepted.")
continue
elif sorting_order < 1 or sorting_order > 2:
print("Invalid input, integer values greater then or equal to one and integer values less then or equal to two are accepted.")
continue
for i in range(random_numbers):
number_list.append(random.randint(0, 999999))
print (number_list)
number_list_min = 0
number_list_max = len(number_list) - 1
if sorting_algorithm == 1:
altered_number_list = selection_sort(number_list, sorting_order)
break
elif sorting_algorithm == 2:
altered_number_list = bubble_sort(number_list, sorting_order)
break
elif sorting_algorithm == 3:
altered_number_list = insertion_sort(number_list, sorting_order)
break
elif sorting_algorithm == 4:
altered_number_list = quick_sort(number_list, number_list_min, number_list_max, sorting_order)
break
except:
print("Invalid input, integer values in the program specified parameters are accepted.")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息:
很可能告诉您,在代码中的某个位置,您必须有一行看起来像:
其中
c
是int
,因此不能拆分为a
和b
就像是一对一样。您的代码中只有一行看起来像这样,而且确实很可疑:
我相信您的意思是交换两个值,而第二个等号
=
应该是逗号,
:请注意,python 给出的完整错误跟踪通常包含的信息不仅仅是这个简短的错误消息。您应该看到一条较长的消息,告诉您在哪个函数以及哪一行发生了错误。阅读完整的错误消息可以更快地找到代码中的可疑行。
The error message:
is most likely telling you that somewhere in your code, you must have a line that looks like:
where
c
is anint
, and thus cannot be split intoa
andb
as if it were a pair.There is only one line in your code that looks like this, and it is indeed suspicious:
I believe you meant to swap the two values, and the second equal sign
=
should instead be a comma,
:Note that the full error trace given by python usually includes more information than just this short error message. You should see a longer message telling you in which function and at which line the error occurred. Reading that full error message would have made it a lot faster to find the suspicious line in your code.