Question
Lab 10 Selection Statements 1 In this lab, we will practice boolean expression, logical expression and simple selection statements. Boolean Expressions: Python has two boolean
Lab 10 Selection Statements 1
In this lab, we will practice boolean expression, logical expression and simple selection statements.
Boolean Expressions:
Python has two boolean value: True and False. A boolean expression is an expression that evaluates to a boolean value. The following relational operators are used in boolean expressions.
Operator | In English |
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
== | equal to |
!= | equal not to |
Activity 1: Assume the variables a = CSC101, b = 4, c = 9.99, and d = True. Please evaluate the following boolean expression. Fill in True or False as the results.
Boolean Expression | Result |
a == CSC110 |
|
b < 2 |
|
c >= 3.25 |
|
d! = False |
|
Boolean Operators:
You can combine boolean expressions using "and", "or", and "not", which are the same as in the English language.
x | y | x and y | x or y | not x |
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
Activity 2: Assume the variables a=2, b=4, and c=6. Fill in True or False for each of the following conditions.
Logical Expression | Result |
a==4 or b>2 |
|
1!=b and c != 3 |
|
a>=-1 or a<=b |
|
not (a>1) |
|
Order of Operations
Boolean operators have an "order of operations" just like mathematical operators. The order is
NAO: not (highest precedence), and, or (lowest precedence)
For example, not x or y and z means (not x) or (y and z)
Activity 3: if A is False, B is True, and C is True, what is the value of A or not B and C?
Show your step by step evaluation step here:
All the programs we have written so far work by executing each statement in the program in order, from top to bottom. To write programs that solve more complex problems, we need mechanisms that will allow statements to be executed in more complex ways. if statements allow us to perform actions only when certain conditions are met, and skip lines otherwise. Lets first review the syntax of an if statement:
if condition:
statements
The first lines of the if statements has three parts: the word if, the condition which must be a Boolean expression, and a colon:
Then, the body consisting of one or more indented lines. The amount of indentation must be consistent.
Activity 4: Please copy the following program into a file called sign.py and run it a few times, entering different numbers (positive, negative, and zero) each time.
number = float(input("Enter a number: "))
if number > 0:
print ("That number is positive.")
What is the output if you enter a positive number?
What is the output if you enter a negative number?
What is the output if you enter a zero?
Now add a second print statement (with any message you want) at the end of the program, being careful to indent it by the same amount the first print statement is indented.
# copy and paste your code here
#How does this affect the output of your program when you enter various values?
Now try removing the indentation from the second print statement. (In other words, leave the first print statement where it is, but move the second one back to be aligned with the if.)
#copy and paste your code here:
#How does this affect the output of your program when you enter various values?
If all went well, you should have discovered that when python encounters an if statement it checks whether the associated condition (in this case, number > 0) is true. If it is, python executes all statements that immediately follow the if clause and are indented beneath it. If the condition is false, these statements are skipped. In either case, execution will then continue with the next un-indented statement.
Notice in particular that to ask whether one value "equals" another value, you must use two equals signs, not one. For example, we could say:
number = float(input("Enter a number: "))
if number == 0:
print( "You entered 0.")
The if statement and relational operators work for comparing strings just like they do for comparing numbers.
Activity 5: Write a program called favorite.py that asks the user to enter a color name. If the color entered happens to be your favorite color, print a message We have the same favorite color!. Otherwise, do not print any response. (Be very careful with punctuation in this program. The if clause must end with a colon, or python will complain.)
# copy and paste your favorite.py here
Notice that to check whether your program works correctly, you will now have to run it at least twice, entering colors that test both outcomes.
Now add one statement to your program that will cause it to print "Goodbye!" just before it ends, regardless of the color entered. Obviously, you also want to test your program to make sure it works.
# copy and paste your whole program here
Activity 6: Let's return to the program sign.py. Suppose we want to print one message when the condition (number > 0) is true, and a different message when the condition is false. We can do that by adding an else clause as shown below. Please modify your copy of sign.py in this way, and do some experiments to convince yourself that it works. (Again, you must be careful to include the colon after else.)
number = float(input("Enter a number: "))
if number > 0:
print("That number is positive.")
else:
print("Definitely NOT positive.")
Now return to favorite.py and modify your program so that it prints one message if the user enters your favorite color, and a different message if not. In addition, your program should print a final message for every user, regardless of what they entered.
# copy and paste your favorite.py here
Activity 7: In most of the Python programs, you will want to interact with the user by the user inputs. For example,
people = int(input("How many people are there?"))
This line of code would work as long as the user enters an integer. However, some input, such as -1, doesnt make sense for the number of people. You program should validate the user input before execute the rest of the code. Test the following program with different inputs.
import sys # import sys module so that we can use sys.exit() to terminate the execution
people = int(input("How many people are there?"))
if people < 0:
print("Invalid input. The number of people must be a non-negative integer.")
sys.exit() #terminate the execution
print("You entered a non-negative integer:", people)
Now, please write a program to ask the user to input a test score, which must be between 0 and 100. Add input validation to your program.
# copy and paste your code here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started