2

This for loop should iterate over even 'lines' in my input, as it holds the users name which I would like to place in a dictionary as a key, with a dictionary inside that key. My output does include the information I needed, but also odd lines which I do not want. I'm new to python so I'm still trying to understand syntax. Here's my code:

def get_name(string_input):
    split_fullstop = string_input.split('.')
    list = [] #creates a list                               
    for line in split_fullstop: 
        count = 0   
        if count % 2 == 0: #if count is even            
            list.append(line.split('is connected to')) #add info to 'list'      
            count += 1 #increase count
        names = {name[0]:{} for name in list}
    return names

And here's the output after printing the function:

{'': {}, 'Levi ': {}, 'Bryant ': {}, 'Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms': {}, 'Olive likes to play The Legend of Corgi, Starfleet Commander': {}, 'Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords': {}, 'Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma': {}, 'Walter ': {}, 'Robin ': {}, 'John ': {}, 'Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man': {}, 'Debra ': {}, 'Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures': {}, 'Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures': {}, 'Ollie ': {}, 'Robin likes to play Call of Arms, Dwarves and Swords': {}, 'Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man': {}, 'Freda ': {}, 'Olive ': {}, 'Mercedes ': {}, 'John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner': {}, 'Jennie ': {}, 'Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game': {}}
2
  • @AntonProtopopov You should have mentioned it as an answer, upvote!! Commented Jan 28, 2016 at 13:15
  • You haven't written what the input is nor what the expected output is. Commented Jan 28, 2016 at 13:31

5 Answers 5

3

Remember that all code at the same indentation level below the for-loop will be run EACH iteration. Therefore you are redefining the variables count and names at each item the for-loop goes through. As mentioned in one of the comments, names should be at the same indentation level as the return statement.

Redefining count at each iteration means you will always find 0 % 2 == 0. It should be defined BEFORE the for-loop. Also, you only increment count when you run the #if count is even portion. So, assuming count is defined before the loop, you will see 0 as even, increment count and be left with an odd value of 1 forever.

Look at looping though indices and values simultaneously using enumerate. That way you need only check the even/odd value of the index.

Sign up to request clarification or add additional context in comments.

Comments

0

maybe your count is a error indentation, count is design to filter even line.

for line in split_fullstop: 
    count = 0   
    if count % 2 == 0: #if count is even            
        list.append(line.split('is connected to')) #add info to 'list'      
    count += 1 #increase count

Comments

0

So you have used a "for each loop", which loops over every element in the iterable and built in functionality to only evaluate the even indexes. Instead of this, I think it is more clear and clean to use the function range.

range(0, len(split_fullstop), 2)

Evaluating only the even

Comments

0

I'll just focus on your count variable, because that's the first thing that tells me there's an error:

for line in split_fullstop: 
    count = 0   
    if count % 2 == 0: #if count is even            
        # some code
    count += 1 #increase count
    #some code
return names

First of all, you're resetting the count variable on each loop with that count = 0 inside the loop, so on EVERY loop count%2 will be equal to 0. This line should be OUTSIDE (before) the loop.

Second, you're increasing the variable inside that if condition count%2 == 0, if in one iteration, count == 0, then it will enter the if-part, and will increase the value to count == 1.
In the next (and all other) iteration, since count == 1, the inside of the if-part won't be executed and thus the count variable won't change.

So, it should be like:

count = 0   
for line in split_fullstop: 
    if count % 2 == 0: #if count is even            
        #some code
        count += 1 #increase count
return names

Comments

0

Note: Don't use built'in variables such as list because you'll overwrite them and it could lead to the unexpected behaviour in future. Use l for example for that.

Your line with names = {name[0]:{} for name in list} will be executed every step because it placed not in the same position as if statement. For steps where count % 2 == 1 you'll add to your dict empty lists. But in your solution you redefine count in each step so you'll never get that as @Isaac Drachman mentioned. So just delete some spaces or tab and define count before for loop:

def get_name(string_input):
    split_fullstop = string_input.split('.')
    l = [] #creates a list            
    count = 0                      
    for line in split_fullstop: 
        if count % 2 == 0: #if count is even            
            l.append(line.split('is connected to')) #add info to 'list'      
            count += 1 #increase count
    names = {name[0]:{} for name in l}
    return names

Or you could rewrite it with list comprehension and enumerate:

def get_name(string_input):
    l = [line.split('is connected to') for i, line in enumerate(string_input.split('.')) if i % 2 == 0]
    names = {name[0]:{} for name in l}
    return names

1 Comment

The names = ... line looks like it's intended to run once at the end over the list that the for loop built. As such, it looks like it needs to be unindented a level, rather than indented another level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.