HW 1 (Python Basics)

Assignment overview

In this assignment, you'll continue practicing Python basics.

Logistics

This is an individual assignment. You are welcome to discuss any part of the assignment with classmates, course staff or Anna. Make sure to cite any help you receive in the "acknowlegdements" portion of the assignment.

This assignment is due at 10PM on Friday, April 3.

Setup

Mount the COURSES drive and create a folder called hw1 in your STUWORK folder. Open the new folder in VSCode and create two files called height.py and functions.py. If you need a refresher on how to complete these steps, refer back to the in-class lab from the first day of class.

Part 1: Sorting by height

For this part of the assignment, you'll write code in height.py.

Python has the ability to group data, for example into pairs (called tuples).

tup1 = ("cat", 3)
tup2 = ("mouse", 5)
tup3 = ("giraffe", 7)

You can make a tuple using variables, as well:

animal = "elephant"
length = 8
tup4 = (animal, length)

There's also a built-in function max that gives us the "largest" of two or more inputs. max behaves logically for numbers, and for strings it compares its values "alphabetically" (it's a little more complex than this, but we'll talk about that later). For example:

a = max(4, 2)
b = max(tup1, tup2, tup3, tup4) # defined in the previous code snippet
print(a) # prints 4
print(b) # prints ('mouse', 5)

You can also give different names to different parts of a tuple (this process is called unpacking):

animalName, nameLength = b # b is ('mouse', 5) so animalName = 'mouse' and animalLength = 5
print("Animal:",animalName)
print("Length:", nameLength)

For this part, write a program in height.py that will ask the user to type in three people's names and their heights. You should make pairs for the names and heights and use the max function to find the tallest person.

For example, if Boris is 68", Miyeon is 70", and Juan is 66", then the program would print that Miyeon is the tallest. In this example, the interaction should look like:

Please enter three people's names and hit enter after each:
Boris
Miyeon
Juan

Please enter their heights in inches and hit enter after each:
68
70
66

Miyeon is tallest (70 inches). 

For full credit, you will need to:

  • Implement a solution without using if statements, even if you know how to use these from a previous class or from reading ahead in the textbook.
  • Match this interaction exactly, including punctuation and spaces
  • Give the correct input even on unrealistic heights (e.g., if the user says that Alice is 66 inches and Bob is 402 inches, your program should correctly report that Bob is taller).

You can test your work by running python3 height.py from the terminal window in VSCode.

Part 2: Functions

For this part of the assignment, you'll write code in functions.py.

Part 2.a Calculations

Create a function calculate. Inside the function, define two variables, num1 with a value of 3, and num2 with a value of 2. Then store the value of num1 * num2 in a new variable called product. (I.e., do this computation in your code. Don't just store the value of 6 directly in product.) Then, still within the function, print the value of product.

Try running calculate directly from the terminal like this:

$ python3
>>> import functions
>>> functions.calculate()

The first line launches the Python interpreter. Make sure to type python3, and not just python. The latter commend will open an older version of Python that we don't want to use. The >>> is how lines within the interpreter start. The line import functions loads the code from function.py into the interpreter. Then, when you run functions.calculate(), the interpreter is able to search the functions namespace for a function called calculate.

Now, your terminal window should look something like this. You can type quit() to exit the interpreter and return to the terminal.

$ python3
>>> import functions
>>> functions.calculate()
The product is 6
>>> 

If you run into errors with any part of this step, first check that your code is indented correctly. Are all lines that belong in the function indented by 1 tab? If you're still running into issues, ask a lab assistant or Anna for help.

Part 2.b Calling a function within your code

Add the following line to the end of functions.py:

calculate()

Make sure this line is outside of the function (i.e., has no indentation).

Now, run the following command from the terminal window:

python3 functions.py

This approach runs all code in functions.py that is not in a function. There are pros and cons to this approach: it's easy to run the module from the command line, but every time you import the module, you'll see the result of running calculate(). (To see the difference, try importing the module in the interpreter now that you've made this change - you should see a difference from what happened before you added this line.)

Part 2.c Fun fact

Write a second function, funFact() in functions.py. This function should contain one line of code that prints a fun fact of your choice. Then, run your code by opening the python interpreter and calling the function directly. For instance, your interaction may look like this:

$ python3
>>> import functions
The product is 6
>>> functions.funFact()
Post-it notes were invented in Minnesota
>>> 

Assignment submission and misc. notes

Final pieces

Remember to add comments, including the header block to each of your files (refer back to HW0 for instructions). You should make sure the acknowledgments section in each header block is accurate. You should include a reflection section in the header of one of the files. Include reflections about both pieces of the assignment there, along with a time estimate of the total time spent working on this assignment.

Handing in the assignment

You need to hand in your solution on Gradescope (see Moodle for the link to Gradescope). You can either click and drag both files into the Gradescope upload window, or you can create a single zip file to submit like this:

zip hw1.zip height.py functions.py

This command means you want to create a zip file called hw1.zip and that it should contain the files height.py and functions.py. You can also use the following shorthand, where * means include all files in the directory:

zip hw1.zip *

Grading

This assignment is worth 20 points, broken up as follows:

  • 8 points Part 1 correctness
  • 4 points Part 2a correctness
  • 4 points Part 2c correctness
  • 4 points style (comments, header, following instructions)

Anna's acknowledgements

Portions of this assignment were borrowed from Tanya Amert and Anna Rafferty. Thanks for sharing!