EX02 - if-then-else


Exercise 00 – To biscuit or not to biscuit?

UNC has a long tradition of basketball. An important part of the tradition is when UNC scores 100 or more points at a game, you can get a free southern biscuit at Bojangles with your ticket.

  1. Create the Python program file
    1. In the comp110/exercises directory create a directory named ex02_if_else
    2. In the comp110/exercises/ex02_if_else directory create a file named biscuits.py
  2. Use the built-in input function to ask the user “How many points did UNC score?” and store their response in a variable called score of type str.
  3. After the prompt, print “UNC scored x points!!”, where x is the value of the score variable.
  4. Create a variable numeric_score of type int to store the numeric representation of the score.
    1. Hint: Use the built-in int function to convert the score string to an integer. Ex: int(score) (documentation: https://docs.python.org/3/library/functions.html#int).
  5. If numeric_score is greater than or equal to 100, print “biscuits!!”. Otherwise, print “no biscuits.”
  6. Be sure to add a docstring to the top of your file describing it in a sentence, as well as an __author__ variable containg your PID in a str.
  • Try running your program from the terminal with python -m comp110.exercises.ex02_if_else.biscuits
    • To rerun your program with that same command after your program completes, press the up arrow key and it should fill in the last command.
    • Generally when working at a Terminal, the up and down arrow keys allow you to navigate your previously run commands.
  • Note: For grading purposes please make sure your output exactly matches the formatting in the directions.

Example program run A:

How many points did UNC score? 110
UNC scored 110 points!!
biscuits!

Example program run B:

How many points did UNC score? 99
UNC scored 99 points!!
no biscuits.

Exercise 01 – The quadratic formula.

  1. In the comp110/exercises/ex02_if_else directory create a program named quadratic.py
  2. Use the input and int built-in functions to prompt the user for 3 integers – a, b, and c
    1. Remind yourself of the quadratic formula if needed! https://en.wikipedia.org/wiki/Quadratic_formula
  3. Compute both roots using the quadratic formula. You are allowed to make use of the built-in sqrt function.
    1. Add import math at the top of your file.
    2. Example usage: math.sqrt(4) would resolve to 2.
    3. Check before taking the square root that the current value of the expression is non-negative. We do not expect you to compute complex roots.
    4. Also, the sqrt function produces a float, so your final answer should be one as well.
  4. Print both roots separated by a comma.
    1. If both roots are real, the output should look like: 4.0, 2.0.
    2. If you have repeated roots, the output should look like: 4.0 repeated.
    3. If you have one complex roots, the output should look like: 2 imaginary roots.
  5. Be sure to add a docstring to the top of your file describing it in a sentence, as well as an __author__ variable containg your PID in a str.

Note: Creating intermediate variables may be helfpul!

Example program run with two real roots:

Enter coefficient 'a' 1
Enter coefficient 'b' -2
Enter coefficient 'c' -4
3.23606797749979, -1.2360679774997898

Example program run with repeated roots:

Enter coefficient 'a' 1
Enter coefficient 'b' -4
Enter coefficient 'c' 4
2.0 repeated

Example program run with imaginary roots:

Enter coefficient 'a' 1
Enter coefficient 'b' 2
Enter coefficient 'c' 3
2 imaginary roots.

Submission

Go ahead and delete any submission zips lingering around in your workspace from the previous exercise.

When you are ready to submit for grading, close out any open Python Debug Console terminals using the Trash Can and then open up a clean, new terminal.

python -m tools.submission comp110/exercises/ex02_if_else

This should produce a submission timestamped with the current date/time for uploading on Gradescope.

Make a Backup Commit

  1. Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
  2. Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
  3. Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
    • If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
  4. In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Finished Exercise 0!!” will suffice.
  5. Press the Check icon to make a Commit (a version) of your work.
  6. Finally, press the Ellipses icon (…) and select “Push” to send this backed up version to your workspace repository space on GitHub.