SmithLogo

CSC 111

Introduction to Computer Science Through Programming

Smith Computer Science



Lecture Notes 08: More Conditionals





Recap: Conditions

Any of the IF blocks can ask about numeric relationships as seen with the operators:

We can also ask about equality or difference with:

Later, we'll see other operators we can use in the conditions.




Recap: Booleans

Boolean values are True or False.

These are the ones that let an IF body get executed or not.

You can directly print them, use them in expressions, or have them get printed as the result of Boolean expression:

print( True ) 
print( False )
print ( (0 < 3) == True )
print ( (-3 > 10) == True )
print( 1 > 2 )





Recap: Cascading IF statements

Remember, from last class:

Think of the following situation:

A professor issues two assignments X and Y.
And the rules for grading are:

  1. If students flunk X (a passing grade is 60), they need a grade of 80 or above in Y to pass the class (C+)
  2. .
  3. If students pass X, and if they also get 80 or above in Y, students get a final grade of A (otherwise C+).


We came up with a flowchart like this:

One answer:
 
Another answer:


Note that the opposite of: \( x < 60\)
is
\( x \geq 60 \)!





So how do we have conditionals that "activate" after the TRUE path of other conditionals?:



We can have conditional statements INSIDE conditional statements

Look at the example shown in the Replit Exercise called ThreeGrades

You can also see the code in the Python Tutor: here




Intervals

We said we could evaluate if a number belongs in the following types of intervals:



How do we evaluate if a number num is in between two other numbers a and b?

Operator chaining

Python supports operator chaining, as in: a < num < b.

Check the following example:
num = 0.5

if 0 < num < 1:
  print ("in between")
elif num==1:
  print ("Max")
else:
  print("Error")


The IF's condition asks if num is in between the values of 0 and 1.

Activity 1 [5 minutes]:
Open the Scratch pad and write a program that
  1. Gets an integer from the user in the range (0 to 100)
  2. Uses Operator Chaining to figure out a Letter grade according to the following rules:
    • A if the grade was in the ange [93 to 100]
    • A- if the grade was in the ange [90 to 93)
    • B+ if the grade was in the ange [87 to 90)
    • B if the grade was in the ange [83 to 87)
    • B- if the grade was in the ange [80 to 83)
    • C+ if the grade was in the ange [77 to 80)
    • C if the grade was in the ange [73 to 77)
    • C- if the grade was in the ange [70 to 73)
    • D+ if the grade was in the ange [67 to 70)
    • D if the grade was in the ange [63 to 67)
    • D- if the grade was in the ange [60 to 63)
    • E Otherwise




Boolean Operators in Conditions

We can also use the following Boolean operators in Python:



Check the following example:
num = 0.5

if num > 0 and num < 1:
  print ("in between")
elif num < 0 or num > 1:
  print ("Outside")
else:
  print("In a limit")


Why would one to do this if we have operator chaining?

Because we can generate complex conditions that can be very specific!

Activity 2 [5 minutes]:
Open the Scratch pad.
You can comment out your previous program or save it in another file! (ASK PABLO ABOUT THIS NOW!))
Now, write a program that:
  1. Gets an integers from the user in the range (0 to 100) and saves it in variable x
  2. Gets an integers from the user in the range (0 to 100) and saves it in variable y
  3. Uses Boolean Operators to figure out one of the following cases:
    1. If x < 60 and y < 80, print "E"
    2. If x < 60 and y > 80, print "C+"
    3. If x > 60 and y < 80, print "C+"
    4. If x > 60 and y > 80, print "A"
You can use any of the IF blocks you like, but try to avoid using any nested IF statements.




Multiple Complex Clauses

Each single section of a condition that can be evaluated to a True or a False is called a Clause.

Using the Boolean operators, we can combine complex clauses into very complex conditions.

There is a tradeof between nesting IF statements and using more complex conditions.
In general, clarity is preferable to compactness, but simple multi-clause conditions are acceptable.




Clauses with other variable types

How do we compare text?

The usual coparisson operators <, <=, ==, !=, >=, and > can be used to compare strings.

The strings will be compared lexicographically (index to index and by order of the letters in the alphablet).

Unicode values of letters in each string are compared one by one.

The effect is comparing their placement according to alphabetical ordering. Activity 3 [5 minutes]:
Open the Scratch pad.
Now, write a program that:
  1. Gets a word from the user and saves it in variable str1
  2. Gets a word from the user and saves it in variable str2
  3. It performs the following actions
    1. Creates the variable str3 as the concatenation of str1 and str2
    2. Prints < the first word> is 'smaller' than < the second word>   If str1 is lexicographicaly smaller than str2
    3. Prints < the second word> is 'smaller' than < the first word>   If str2 is lexicographicaly smaller than str1
    4. Prints < the first word> is 'equal' to < the second word>   If str1 is lexicographicaly equal to str2
the following is a good read about comparing strings




The in Operator

In Python, the in operator in checks if a specified value is part of a sequence like a string. It also works with thing's we'll see soon, like arrays, lists, or tuples.

An example is the following:

hangman_word = "abracadabra"
guess = input("give me a letter: ")
if (guess in hangman_word):
  print("you guessed right!")
else:
  print("You guessed wrong!") 


This works for values in general!, not just single letters, so a word might be found inside a sentence!

Activity 4 [5 minutes]:
Open the Scratch pad and write a program that:

  1. Gets a string from te user
  2. Gets another string from the user
  3. Your program should return whether or noth the first string is inside the second one.




Single-Round Battleship

Activity 5 [5 minutes]:

Open the Scratch pad and write a program let's you play one round of Battleship!.

It must do the following:
  1. Defines two strings with the following (hardcoded) values: :
    • ship01_start: a letter in [A-Z] concatenated with a number in [0-9]; example 'B4'
    • ship01_end: a letter in [A-Z] concatenated with a number in [0-9]; example 'B6'
  2. Gets a letter from the user in the range (A to Z)
  3. Gets an integer from the user in the range (0 to 9)
  4. Uses any of the instructions we learned today to figure out if the user input "Hits the Ship!"
HINT:
A ship that is between 'B4 and B6' would be hit if the user indicates a letter 'B' and any number between 4 and 6.
A ship that is between 'C7 and E7' would be hit if the user indicates the number '7' and the indicated letter is in the range of letters "CDE".




Homework

[Due for everyone]
Homework 03 is due on Friday, 02/18 at 5PM

[Optional] ZyBooks Sections 3.2, 3.4, 3.5