Suppose we want to control our Pypet. Start a file named yourname_temp. Type this in your temp file and then run it several times.
if/else structure
- a colon goes after the if statement, elif statement, and else statements
- blocks with actions that follow these must be indented consistently
- if and elif (else if) contain a test statement that is either True or False
Challenge: Improve upon your Pypet program.
Make use of +=.
Add hungry = hungry.upper() so that if the user enters lowercase 'y' or 'n' it still works.
Add a statement to show the amount of food at the end.
Add code before the else statement that uses elif hungry == "N" and follow it with a print statement.
Raise your hand for a check when you get to this point.
Tests using Comparison Operators
- Greater than, Less than, Greater than or equal to >, <, >=
- Equal ==
- Not equal !=
Try these in the console, or shell:
Logical Operators
Try these in the console, or shell. Remember to guess the outputs first.
Every object actually has a boolean value and can be used as a test. This means that every number, string, etc. has a value of True or False. Most of them are considered True.
Think of an object as True if it is not empty or zero.
Try
Try this in your temp file. You can delete prior work in the temp file as we try things there.
Next try this in your temp file. Run it several times.
Challenge: Guess at the output of these and then try them in your temp file.
Try
Try
Try
We'll discuss these together.