In Python, you can ask the computer to make decisions based on events. This is similar to deciding which shirt to wear on school picture day. If it’s school picture day, you have to wear a formal attire. If it isn’t school picture day, you can wear a casual attire. Here is an example below.
Notice how I used 2 equal signs. I did this because Python wants to differentiate between comparing things versus assigning variables. When assigning variables, you use one equal sign. When comparing things, you use two.
Also notice how I gave 4 spaces after the if and else statements. This is to show that the next line after the if/else statement is part of that statement.
If you have an if statement where you want to do nothing if the statement is false, you don’t need the else statement as shown below.
The same way, if you have an if statement with many conditions. Then you can use elif statements to show that if the previous statement is false, you have an other statement that you can check. Else is only used when all previous statements are false.
Referring to the short interview you wrote in the previous lesson, add to that using if conditions and testing for different answers the user gives.
Copyright © 2024 PieceOfAPie - All Rights Reserved.
Powered by GoDaddy
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.