所以我做了一个代码,要求用户交换列表中的两个地方,直到列表从最小到最大。因此,它应该是这样的:有用户排序列表从最小到最大
Hello: Your current list is [6, 7, 8, 2 , 9, 10, 12, 15, 16, 17]
Please pick your first location -> 4
Please pick your second location -> 2
Your new list is [6, 2, 8, 7 , 9, 10, 12, 15, 16, 17]
我已经得到了这部分,但我目前还无法弄清楚如何获得用户来进行排序,而不是代码。
Your list is not sorted: Please continue
Please pick your first location -> 1
Please pick your second location -> 2
Your new list is [2, 6, 8, 7 , 9, 10, 12, 15, 16, 17]
Please pick your first location -> 3
Please pick your second location -> 4
Your new list is [2, 6, 7, 8 , 9, 10, 12, 15, 16, 17]
Great job, thank you for sorting my list.
这里是我的代码:
list = [4,2,5,5,6,4,7,6,9,5]
print("Heres your current list", list)
print("Pick a location between 1 and 10")
num = int(input())
if num <= 10 and num >= 1:
print("Please pick another location between 1 and 10")
num1 = int(input())
tempBox1 = list[num-1]
tempBox2 = list[num1-1]
list[num-1] = tempBox2
list[num1-1] = tempBox1
print("Your new list is", list)
请[编辑]让你的缩进是正确的。请使用空格而不是制表符。 –
看起来不错,为什么不对'list'进行排序并将其设置为像'sorted_list'这样的变量,并继续执行您的操作,还要检查每个用户输入,直到列表等于'sorted_list'为止 – davedwards
使用'list'作为变量标识符可能会产生误导。改用'lst','L','my_list'等。 – Tonechas