所以我做了一個代碼,要求用戶交換列表中的兩個地方,直到列表從最小到最大。因此,它應該是這樣的:有用戶排序列表從最小到最大
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