Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to use a True and False

+2 votes
asked Jan 30, 2019 by Oleksandr Khanenko (140 points)
Hi I'm new in python and I would like to ask how use a True and False I ready now what True is 1 and False is 0 but I can't find more information about it how use it when you are coding or when you create the site share me with your idea.

Thank you

1 Answer

0 votes
answered Jan 31, 2019 by Ryan (1,230 points)
edited Jan 31, 2019 by Ryan

True and False can be used for many things. For starters, they can be used to tell a loop how long to execute. Take a look at this code:


executeLoop = True
i = 0

while (executeLoop): # This means the loop will execute while executeLoop is true
    i = i + 1
    print(i)
    
    if (i == 5): # This statement stops loop execution when i equals 5
        executeLoop = False
        continue # Skip directly to the top of the loop to quickly exit


If you don't understand all of this code yet, that's okay. In this code, the while loop only executes as long as the variable executeLoop equals True.

You can also use variables holding True or False to hold specific values for your program. For example, if you are making a game and no one has one yet, you could have a variable called "winner", and set it to False. When someone has one, you can set it to True.

Hope this helps!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...