**CS 1109: Fundamental Programming Concepts (Summer 2024)** [Home](../index.md.html) • [Syllabus](../syllabus.md.html) • [Schedule](../schedule.md.html) • [Assignments](../assignments.md.html) • [Labs](../labs.md.html) (#) Lab 2: Conditionals In this lab, you will gain experience working with writing and tracing conditional statements in Python. !!! note: Due Date This lab is due **Tuesday, July 2, 2024 at 11:30 am**. (##) Exercise 1: Tracing Conditionals In this exercise, you will get practice reading Python code with conditional statements. Consider the following program and answer the following questions with your partner: ~~~ python listing linenumbers num = int(input("Enter an integer: ")) if num == 5: print("red") elif num < 5: if num >= 0: print("green") else: print("blue") else: if num > 10: print("purple") ~~~ Please write your answers with interval notation. !!! note: Interval Notation * 0 < x < 1 would be written as (0,1) * 0 <= x < 1 would be written as [0,1) * 0 < x <= 1 would be written as (0,1] * 0 <= x <= 1 would be written as [0,1] * 0, by itself, would be written as [0,0] * You can express infinity with a dash (`-`), plus(`+`), or `inf`, such as [-, 5) or (0, inf]. 1. What values of `num` will make the program print `red`?
2. What values of `num` will make the program print `green`?
3. What values of `num` will make the program print `blue`?
4. What values of `num` will make the program print `purple`?
5. What values of `num` (if any) will make the program print nothing?
6. Is the following program equivalent to the one above? Explain your answer by checking how the values you found in the previous questions behave in this program.
~~~ python listing linenumbers num = int(input("Enter an integer: ")) if num < 0: print("blue") elif num < 5: print("green") elif num == 5: print("red") elif num > 10: print("purple") ~~~ 7. Which program do you prefer? (##) Exercise 2: GPA to Letter Grade Converter The official grading system for Cornell University can be found [here](https://courses.cornell.edu/content.php?catoid=55&navoid=22442). Consider a simplified system that relates grade-point-averages (GPA) to letter grades: - Greater than or equal to 3.7: **A** - Between 2.7 (inclusive) and 3.7 (exclusive): **B** - Between 1.7 (inclusive) and 2.7 (exclusive): **C** - Between 0.7 (inclusive) and 1.7 (exclusive): **D** - Less than 0.7: **F** Write code which prompts the user for their GPA, a `float` between 0.0 and 4.0, and prints the corresponding letter grade associated with that GPA value. If the user provides a GPA that is invalid (i.e., less than 0.0 or greater than 4.0) then the program should print an error message informing the user that they must enter in a value between 0.0 and 4.0. You may write any message you'd like, but for example, ~~~ python listing Enter your GPA: 1000.0 That is an invalid GPA. Your GPA must be between 0.0 (inclusive) and 4.0 (inclusive). ~~~ This is a form of *input validation*. Input validation is where you check that the input the program received from the user is of the desired form. It is a good programming habit to validate user input whenever you solicit it. This will prevent unforseen errors down the road! (##) Exercise 3: Cleaner Conditionals Many of you likely wrote a guard similar to this to check whether the GPA corresponds to a "C" letter grade: ~~~ python listing gpa < 2.7 and gpa >= 1.7 ~~~ However, it is possible to rewrite the guard without using `and`. How? Consider code that came before this line. (##) Exercise 4: Equivalent Conditionals A guard is another way of calling a boolean statement. It is called a guard because the statement "guards" the indented code beneath and only gets executed if the guard (condition) is satisfied (True). 1. Consider the following two programs. Are they equivalent? Why or why not? **Program A:** ~~~ python listing if guard1: if guard2: foo() ~~~ **Program B:** ~~~ python listing if guard1 and guard2: foo() ~~~ 2. Consider the following two programs. Are they equivalent? Why or why not? **Program C:** ~~~ python listing if guard1: foo() if guard2: foo() ~~~ **Program D:** ~~~ python listing if guard1 or guard2: foo() ~~~ 3. Consider the following two programs. Are they equivalent? Why or why not? **Program E:** ~~~ python listing if guard1: foo() elif guard2: foo() ~~~ **Program F:** ~~~ python listing if guard1 or guard2: foo() ~~~ (##) Exercise 5: Detect evens and odds Write code that takes in a number from a user and then prints out whether it is an even or odd number (zero is even). Hint: remainders are involved. (##) Exercise 6: Truth table Create a truth table for the following simple logic: ``` A and not B ``` Use the table and fill in values of T for True and F for False: ``` # A | B | not B | A and not B # --------------------------- # | | | # | | | # | | | # | | | ``` (#) Instructions For Submitting Please name your file `lab2_.py` and add this header to your lab 2 Python file: ~~~~~~~~~~~~~~~~~~~~~~~~~ Python listing # Lab 2 # YOUR NAME (netid) # ~~~~~~~~~~~~~~~~~~~~~~~~~ Please answer the questions in this lab using the following format: ~~~~~~~~~~~~~~~~~~~~~~~~~ Python listing ### Exercise # 1. Non-code answer here # 2. Non-code answer here ### Exercise Your code here ~~~~~~~~~~~~~~~~~~~~~~~~~ Upload your `lab2_.py` to [Canvas](https://canvas.cornell.edu/courses/64874/assignments/) under the assignment for this lab. ------------------------------------------------------------------------------- ![ ](../assets/img/cc-by-sa.png) Unless specified elsewhere on this page, this work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/). -------------------------------------------------------------------------------