list - What's wrong with my for loop? Python -
this loop should iterate on 'lines' in input, holds users name place in dictionary key, dictionary inside key. output include information needed, odd lines not want. i'm new python i'm still trying understand syntax. here's code:
def get_name(string_input): split_fullstop = string_input.split('.') list = [] #creates list line in split_fullstop: count = 0 if count % 2 == 0: #if count list.append(line.split('is connected to')) #add info 'list' count += 1 #increase count names = {name[0]:{} name in list} return names
and here's output after printing function:
{'': {}, 'levi ': {}, 'bryant ': {}, 'jennie likes play super mushroom man, dinosaur diner, call of arms': {}, 'olive likes play legend of corgi, starfleet commander': {}, 'debra likes play 7 schemers, pirates in java island, dwarves , swords': {}, 'levi likes play legend of corgi, 7 schemers, city comptroller: fiscal dilemma': {}, 'walter ': {}, 'robin ': {}, 'john ': {}, 'walter likes play seahorse adventures, ninja hamsters, super mushroom man': {}, 'debra ': {}, 'freda likes play starfleet commander, ninja hamsters, seahorse adventures': {}, 'mercedes likes play legend of corgi, pirates in java island, seahorse adventures': {}, 'ollie ': {}, 'robin likes play call of arms, dwarves , swords': {}, 'bryant likes play city comptroller: fiscal dilemma, super mushroom man': {}, 'freda ': {}, 'olive ': {}, 'mercedes ': {}, 'john likes play movie: game, legend of corgi, dinosaur diner': {}, 'jennie ': {}, 'ollie likes play call of arms, dwarves , swords, movie: game': {}}
remember code @ same indentation level below for-loop run each iteration. therefore redefining variables count
, names
@ each item for-loop goes through. mentioned in 1 of comments, names
should @ same indentation level return
statement.
redefining count
@ each iteration means find 0 % 2 == 0
. should defined before for-loop. also, increment count
when run #if count even
portion. so, assuming count
defined before loop, see 0
even, increment count
, left odd value of 1
forever.
look @ looping though indices , values simultaneously using enumerate
. way need check even/odd value of index.
Comments
Post a Comment