我正在尝试制作一个程序,将播放器的输入添加到按字母顺序排序的列表中。当我尝试这个程序时,我想出来就是随便安排一下这个词,我不知道有什么不对。我错过了什么?我完全清楚有一个功能可以为你排序,我只是在练习这个。
alphabet = 'abcdefghijklmnopqrstuvwxyz'
array = []
while True:
pl_inp = input('Add something to the list:\n')
location = 0
list_has_content = False
for x in reversed(array):
indx = 0
if alphabet.find(pl_inp[indx]) == alphabet.find(x[indx]):
indx += 1
elif alphabet.find(pl_inp[indx]) > alphabet.find(x[indx]):
list_has_content = True
location = array.index(x)
elif alphabet.find(pl_inp[indx]) < alphabet.find(x[indx]):
location = array.index(x) + 1
break
if location == 0 and list_has_content == True:
array.insert(location, pl_inp)
elif location > 0:
array.insert(location, pl_inp)
else:
array.append(pl_inp)
print(array)