我需要幫助,爲我的任務額外增加一部分功能。目標是製作一個列表,然後允許用戶輸入他們自己的數據(在這種情況下是鳥類),然後將其排序並返回鳥類。額外的信貸部分是允許用戶在之後編輯任何信息。我不知道如何查找/替換用戶提供的內容。使用用戶輸入查找並替換列表(python)
代碼:
def sorted_list():
bird_list.sort()
for i in bird_list:
print(i)
print()
print('There are', len(bird_list), 'birds in the list.')
#end for
#end def
cond = 'y'
while cond == 'y':
bird = input('Type the name of another bird (RETURN when finished): ')
if bird in bird_list:
print(bird, 'is already in the list.')
else:
bird_list.append(bird)
print(bird, 'has been added to the list.')
if bird == '':
cond = 'n'
sorted_list()
#end if
#end while
edit = input('Edit? (y/n) ')
print()
if edit == 'y':
change = input('Which bird would you like to change? ')
if change == bird_list[0]:
i = input('Enter correction ')
else:
print('Entry not found in list')
編輯:
使用該
if edit == 'y':
change = input('Which bird would you like to change? ')
if change in bird_list:
loc = bird_list.index(change)
bird_list.remove(change)
correction = input('Enter correction ')
bird_list.insert(loc, correction)
else:
print('Entry not found in list')
那麼我的嘗試是它所說的編輯下面的一切。但是會發生什麼呢,比如說我輸入了烏鴉,這個烏鴉會在列表中出現0,我得到'列表中沒有找到條目' – 2014-10-20 01:49:54