CSCI 141 - Lab 3

Scott Wehrwein

Spring 2025

In this lab, you will implement two programs that make use of conditional statments (if, if/else, if/elif/else).

1. A Quadratic Formula Solver

You’ve been tasked with writing a homework help program that can be used to verify students’ calculations involving the quadratic formula.

Write a program called quadratic.py that takes three floating-point numbers as command line arguments representing the coefficients \(a\), \(b\), and \(c\) in the quadratic equation \(y = ax^2 + bx + c\), and print out the roots of the parabola represented by this equation.

It’s been a while since I took algebra class, so in case you, too need a refresher: a quadratic equation of the form \(y = ax^2 + bx + c\) takes the shape of a parabola; the roots of the quadratic are the values of \(x\) for which \(y = 0\). The so-called “quadratic formula” for finding these roots (i.e., the values of \(x\) where the parabola intersects the \(x\) axis) is: \[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]

How do I compute the square root of a number in Python? There are a couple ways to do it, but the one I recommend here uses a fact you may remember from math class: \(\sqrt{x} = x^{(\frac{1}{2})}\). In other words, raising a number to the power of \(0.5\) is equivalent to taking its square root.

Some facts to remember about this formula:

Your program should behave as follows to handle the special cases described above:

Here are a few sample runs of quadratic.py:

Testing

You should be sure to make sure your code behaves correctly in the following situations:

2. Eligibility for Office

In this program, you will help a user determine whether they meet the eligibility requirements to run for certain legislative offices. From Wikipedia, here are the eligibility rules for running for US Representative and US Senator. I left out the third qualification requiring residency of the state in which they are running for office - we’re not testing that qualification with this program.

You will write a program that tells the user if, based on their age and years of citizenship, they are eligible to run for each house of Congress in the United States.

  1. Nested ifs vs Boolean Operators

    One approach to solving the problem would be to use nested if statements. You could put an if statement inside of another if statement to test two different conditions. Consider the following code snippet that uses nested if statements.

    x = 9
    if x < 10:
       if x > 0:
          print("you entered a single digit positive number.") 

    Notice that the print statement will execute only when \(x\) is less than \(10\) and \(x\) is greater than \(0\). Also notice that I used the word “and” in the previous sentence.

    An alternative way (that is logically equivalent) to write the code segment above is:

    x = 9
    if x < 10 and x > 0:
          print("you entered a single digit positive number.") 

    Notice the correspondence between nesting if statements and using the Boolean operator and.

    Using those ideas, you’re going to write a program that checks to see if you’re eligible to run for office.

  2. Sample Output for one person Here a few sample runs of my program. The numbers at the end of the first two lines are entered by the user.

    Enter the age of the person. 28
    How many years have they been a citizen? 11
    They can run for Representative
    
    Enter the age of the person. 58
    How many years have they been a citizen? 22
    They can run for Senator or Representative
    
    Enter the age of the person. 19
    How many years have they been a citizen?  18
    They are not eligible to run for either office.

    For this problem, your program’s output should match the sample output exactly.

  3. Writing pseudocode is a great way to get your ideas on paper in a semi-structured way to help guide you when you write actual code. I often write pseudocode before I write Python because it helps me think through the steps I need to take before I start thinking about the details of expressing those steps in Python.

    A lot of programmers like using a whiteboard to sketch out a flowchart of the code to make sure they understand the basic flow, then they block in the parts as comments. Finally, they gradually sketch in different parts of the code, knocking off one part at at time. If this hasn’t been your process before, try it now! Future assignments will be get increasingly difficult to complete without some process for carefully breaking down your thinking about your solution approach, so it’s good to practice it on an easier assignment.

    Here is some pseudocode I wrote for this program:

    ask for the person's age 
    ask for the person's years of citizenship
    
    if they are eligible to run for Senator, tell them
    else if they are eligible to run for Representative, tell them
    else tell them they are not eligible for either

    Notice that each line of pseudocode is (1) precise enough to translate into code and (2) simpler and easier to translate into code than the higher-level description of the problem above.

  4. Test your code Any time you write code you should test it thoroughly. It’s more important to test code carefully than simply test a lot of random cases. This program has several different outcomes: plan some test cases which would end up in each outcome.

    In this particular example, test the program by inputting data that falls into each of the three scenarios (i.e. (1) Senator or Representative, (2) Representative, (3) neither. You should also take a moment to convince yourself that it’s impossible (and therefore you don’t need to test) for a person who is eligible to be a Senator but not a Representative.

    The easiest way to do this is to write down a list of “test cases” for various inputs and what the resultant output should be. You can then check to see if your code matches.

Submission

  1. As usual, make sure you have a comment to the top of your code that lists your name, the date, and a short description of the program’s purpose.

  2. Submit quadratic.py and representative.py to the Lab 3 assignment on Canvas.

Rubric

This lab is worth a total of 20 points:

quadratic.py (5 points)

representative.py (5 points)

Possible Deductions

Acknowledgements

Thanks to Dr. Perry Fizzano and Dr. Caroline Hardin for developing materials on which this lab is based.