Finding and replacing in a list using user input (python) -
Finding and replacing in a list using user input (python) -
i need help credit portion of assignment. objective create list , allow user input own info (in case birds) , sort , homecoming birds. credit portion allow user edit info after. don't know how find/replace user gives it.
code:
def sorted_list(): bird_list.sort() in bird_list: print(i) print() print('there are', len(bird_list), 'birds in list.') #end #end def cond = 'y' while cond == 'y': bird = input('type name of bird (return when finished): ') if bird in bird_list: print(bird, 'is in list.') else: bird_list.append(bird) print(bird, 'has been added list.') if bird == '': cond = 'n' sorted_list() #end if #end while edit = input('edit? (y/n) ') print() if edit == 'y': alter = input('which bird change? ') if alter == bird_list[0]: = input('enter correction ' ) else: print('entry not found in list')
edit:
resolved edit issue using this
if edit == 'y': alter = input('which bird change? ') if alter 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')
first, can utilize .index
find item's position in list.
but there problem in code, reason got 'entry not found on list'
output when come in name @ index 0 in list, first time come in blank string(enter enter
key without input nothing), append blank string bird name in bird_list
, , sorted_list
method sort blank string ''
in first place of list, here:
if bird in bird_list: print(bird, 'is in list.') # if bird ''(first time), appended list, else: bird_list.append(bird) print(bird, 'has been added list.') if bird == '': cond = 'n' # , sort '' in 0 index of list sorted_list()
the right logic should be:
if bird in bird_list: print(bird, 'is in list.') elif bird != '': bird_list.append(bird) print(bird, 'has been added list.') else: cond = 'n' sorted_list()
python list
Comments
Post a Comment