Python Forum
I have a problem with the IF Statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I have a problem with the IF Statement
#1
        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 : ?|
buran write Nov-11-2024, 07:34 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Please use bb tags when posting code.
What is chooseTerm?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
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 ]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with while statement. BobSmoss 3 2,532 Jan-08-2022, 03:22 PM
Last Post: BobSmoss
  Problem in if-else statement shantanu97 2 3,067 Apr-09-2021, 06:37 AM
Last Post: shantanu97
  multiple condition if statement problem FelixReiter 3 3,430 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  Problem with If statement and dataframe Milfredo 1 2,275 Sep-16-2020, 05:50 AM
Last Post: Milfredo
  Problem with If else statement Milfredo 5 3,526 Aug-30-2020, 06:32 PM
Last Post: Milfredo
  Problem with a 'break' statement. christopher3786 3 3,299 Jun-20-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Problem with an IF statement Ryan_Todd 13 7,098 Jan-30-2020, 08:22 PM
Last Post: snippsat
  Problem with 'and' in 'if' statement CoderMan 3 3,436 Oct-06-2019, 07:32 PM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,947 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Problem with elif statement Haddal99 2 3,159 May-20-2019, 09:26 AM
Last Post: avorane

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020