Python Forum

Full Version: I have a problem with the IF Statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
        colors1=[ ]
        colors2=[ ]
        colors3=[ ]
        for i in range(0,49):
            if colors1[ i ]==chooseTerm:
                colors2.append( i )
                colors3.append(chooseTerm)
                print(colors2," # LIST")
                print(colors3," the Color List")
                break        
That's my script since dictonaries won't update in an IF statement. And, this won't work either : ( . All my variable are global.
Is something Wrong with IDLE here ? My program was working fine and I tinkered with it and then tried to fix it and had to start from scratch. Now it seems like my IF statements don't want to work. Is it IDLE or me : ?|
Please use bb tags when posting code.
What is chooseTerm?
Please, post full, working example.
Also, note right now the loop wll break out after first iteration, because of the break
Also colors1 is empty, never populated and there should be IndexError on colors1[ i ]
This is incorrect:
Quote:since dictonaries won't update in an IF statement
Code in the body of an if statement is no different than code outside an if statement.

Looking at your code it looks like you are trying to make a mapping between colors and their location in colors1. Is the problem with dictionaries that they only map one value to each key? If so, you can use a list as the value of a dictionary. Like this:
from collections import defaultdict


colors1 = ['red', 'black', 'blue', 'green', 'red', 'black', 'blue', 'green', 'red', 'black', 'blue', 'green']
colors2 = defaultdict(list)

for index, color in enumerate(colors1):
    colors2[color].append(index)

print(colors2)
Output:
defaultdict(<class 'list'>, {'red': [0, 4, 8], 'black': [1, 5, 9], 'blue': [2, 6, 10], 'green': [3, 7, 11]})
Quote:Is something Wrong with IDLE here ?
There are many things wrong with IDLE, but they probably don't affect if statements. Most problems you'll see running code in IDLE have to do with IDLE replacing some python functions with special IDLE versions. I've had trouble running code that moves the text cursor around or changes display fonts and colors. Tkinter programs run differently in IDLE. Pygame programs too. If you have doubts about IDLE affecting how your code runs, run it from the command line and see if it runs differently.

You should post your actual code. It is difficult to understand your questions about code when the code you post doesn't do anything.