![]() |
| I have a problem with the IF Statement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I have a problem with the IF Statement (/thread-43530.html) |
I have a problem with the IF Statement - chlyde01 - Nov-11-2024 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") breakThat'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 : ?| RE: I have a problem with the IF Statement - menator01 - Nov-11-2024 Please use bb tags when posting code. What is chooseTerm? RE: I have a problem with the IF Statement - buran - Nov-11-2024 Please, post full, working example. Also, note right now the loop wll break out after first iteration, because of the breakAlso colors1 is empty, never populated and there should be IndexError on colors1[ i ] RE: I have a problem with the IF Statement - deanhystad - Nov-11-2024 This is incorrect: Quote:since dictonaries won't update in an IF statementCode 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) 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. |