(see the video at: https://youtu.be/isLtkpgHM8Y)
One of the things that makes a computer more powerful than a calculator is the ability to do different things based on circumstances. This ability to make decisions is called conditional processing. For example:
What is 1 + 1? 2
Your answer is correct.
Here is how you do it:
inp = int(input("What is 1 + 1? "))
if inp == 2:
print("Your answer is correct.")
You can think of this as a stimulus/response,
such as if you are hungry, you should eat. The first part, the stimulus, is called the condition, and in the above example is the
line that says
if inp == 2: – note
a few details: (1) It starts with the word
if; (2) it’s followed by a relational
statement (explained below); and (3) the entire line is ended with a colon.
The relational statement is a statement where two values are compared; in the above example, the value of inp is compared to the number 2. In this case, the relational operator is
== (which stands for equal to). It is very important to note that if you want to compare something for equality, you have to use the double equals symbol
==. Why? Because a single equals sign
= is what you use for an assignment statement (which you’ve been doing for a number of problems now).
Other relational operators are:
> (greater than),
< (less than),
!= (not equal to),
<= (less than or equal to), and
>= (greater than or equal to).
After the condition line, you must include one or more lines of code which are the response to the stimulus. In the above case, this is printing the message
Your answer is correct. This line needs to be indented under the conditional statement or it will not work properly. (In my experience, this is one of the aspects of Python that gives students the most constant trouble.) If there was more than one line of code to execute as the response to the stimulus, you would need to indent each of the lines under the conditional statement. The line or lines of code under the condition statement are called a code block. This means “block” like “block of wood” not like “block your path.”
Write a program that prompts the user for an even number from 2 to 100. If the value is equal to 82, display “indigo”.
|Test set # 1 82||Test set # 2 78||Test set # 3 94|
|Test set # 4 92||Test set # 5 50||Test set # 6 100|
|Test set # 7 74||Test set # 8 80||Test set # 9 32|
Please enter an even number from 2 to 100: 82
Please enter an even number from 2 to 100: 78
Write a program that prompts the user for a Fibonacci number. If the value is less than 2584, display “waiter”.
|Test set # 1 2584||Test set # 2 6765||Test set # 3 1|
|Test set # 4 55||Test set # 5 89||Test set # 6 987|
|Test set # 7 5||Test set # 8 17711||Test set # 9 1597|
Write a program that prompts the user for a number from 1 to 20. If the value is less than 12, display “understand”.
|Test set # 1 12||Test set # 2 19||Test set # 3 16|
|Test set # 4 5||Test set # 5 14||Test set # 6 20|
|Test set # 7 6||Test set # 8 8||Test set # 9 9|
Write a program that prompts the user for an even number from 2 to 100. If the value is not equal to 94, display “frost”.
|Test set # 1 94||Test set # 2 30||Test set # 3 64|
|Test set # 4 62||Test set # 5 4||Test set # 6 10|
|Test set # 7 92||Test set # 8 14||Test set # 9 90|