We just have to be sure that we are aware of our indentation. There is no limit to the amount of times that we can nest. However we will also be looking at the following Boolean. answer = input ( "What is your age? " ) if int ( answer ) >= 18 : answer = input ( "What country do you live in? " ) if answer = "canada" : print ( "Me as well!" ) else : print ( "Oh, I do not live there." ) A while loop is a programming concept that, when it's implemented, executes a piece of code over and over again while a given condition still holds true. The conditional statement checks to see if a statement is True or False. For example placing an if statement inside of an already existing if statement. print () is a function in Python 3 so I assumed you are using Python 3. print is a statement instead of a function. Nesting is simply putting code inside of other code. input in Python 2 will tries to interpret the input as Python code. Now that we've learned about some basic logic and control structures in python we can move on to nesting. Of course we can combine this with our knowledge of if statements to check multiple conditions in one if: food = input ( "What is your favorite food? " ) drink = input ( "What is your favorite drink? " ) if food = "pizza" and drink = "juice" : print ( "Those are my favorite as well!" ) else : print ( "One of those is not my favorite" ) Nested Statements We can also combine the use of these keywords to create longer conditions: ( True or False ) and False # This is False False and True and True # This is False ( True or False ) and True # This is True True and not ( False ) # This is True not ( 1 > 2 and 2 - 7 = - 5 ) # This is True True and False # This gives False True and True # This gives True False and False # This gives False True or False # This gives True True or True # This gives True False or False # This gives False not True # This gives False not False # This gives True If the condition is true it will give us a false value (you can think of it as reversing the condition). If the condition is false it will result in a true value. The not keyword allows us to check if an entire condition is false. If both of the conditions are false then the entire condition is false. If one or both of the conditions are true then then entire condition will be true. The or keyword allows us to check if one of two conditions is true. If one or both of them are false then the entire condition is false. ![]() If they are both true then the entire condition is true. The and keyword allows us to check if two conditions are true. We can combine conditions using the following three key words: When I program conditional statements, I use Python syntax. Chained conditionals are simply a "chain" or a combination or multiple conditions. It is a statement that if something is true then do something.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |