Assignment 1 - Hello, Python!
Due: Thursday, March 28, 2024, at 4pm
You may work alone or with a partner, but you must type up your code yourself. You may also discuss the assignment at a high level with other students. You should list any student with whom you discussed the assignment, and the manner of discussion (high-level, partner, etc.) in top-level comments within your source code file.
You should submit your assignment as an intro.py
file on Moodle at this link.
Goals
The primary goal for this assignment is to get you up and running with Python. You will need to make sure you can write and execute Python programs to complete this assignment and test it; the actual code you write will be quite simple.
Part 0: Comments and collaboration
# You should be equipped to complete this part after Lesson 1 (Monday Mar. 25).
For each assignment in this course, you are expected to provide top-level comments (lines that start with #
at the top of the file) on any .py
source code files with your name and a collaboration statement. For this assignment, your program should be named intro.py
.
Here is an example of how you can start your file:
# File: intro.py
# Author: Lulu Amert
# Purpose: Assignment #1 - Hello, Python!
#
# Collaboration statement:
# - Milo: partner (worked on code together)
# - Hobbes: discussed issues with spaces in print() output
# - Slack: link to Python documentation for print()
Here is another example:
# File: intro.py
# Author: Hobbes Amert
# Purpose: Assignment #1 - Hello, Python!
#
# Collaboration statement: I worked alone.
Note: You should not be using search engines when completing your assignments. You can, however, search within the official Python documentation: https://docs.python.org/3/.
Part 1: Hello, user!
# You should be equipped to complete this part after Lesson 1 (Monday Mar. 25).
For this part, you should write a program that asks the user for their first and last name, and then greets them by name. Your program should also ask their age and give them an estimate on the number of days old they are (assuming exactly 365 days per year). This code should live in the intro.py
file you started in Part 0.
Here is an example interaction that your program should be able to mimic:
Hello there!
What is your first name? Lulu
What is your last name? Amert
How old are you, in years? 12
Have a great day, Lulu Amert!
You are approximately 4380 days old.
Here is another example:
Hello there!
What is your first name? Biscuit
What is your last name? Amert
How old are you, in years? 3
Have a great day, Biscuit Amert!
You are approximately 1095 days old.
Part 2: Early mornings
# You should be equipped to complete this part after Lesson 1 (Monday Mar. 25).
Python has the ability to group data, for example into pairs (called tuple
s):
tup1 = ("cat", 3)
tup2 = ("mouse", 5)
tup3 = ("giraffe", 7)
You can even make one of these pairs using variables:
animal = "elephant"
length = 8
tup4 = (animal, length)
There is also a handy built-in function min
that can give us the “smallest” of two or more inputs. This behaves logically for numbers, and for strings it compares “alphabetically” (it’s more complicated, but we’ll explore that next week). For example:
a = min(4, 2)
b = min(tup1, tup2, tup3, tup4) # defined in the previous code snippets
print(a) # prints 2
print(b) # prints ('cat', 3)
For this part, you should extend your program to additionally ask the user to type in three students’ names and earliest class times on Mondays/Wednesdays/Fridays (e.g., the user will type 2a
or 6a
). You should make pairs for students’ names and class times and use the min
function to find the student with the earliest classtime.
Note: For full credit, you must use min
and not use any if
statements, even if you already know about them from prior CS experience.
For example, if Boris has a 2a, Miyeon has a 3a, and Juan has a 1a, then the program would print out that Juan has the earliest class (1a). In this example, the interaction should look like this:
Please enter three students' names and hit enter after each:
Boris
Miyeon
Juan
Please enter their earliest classtimes and hit enter after each:
2a
3a
1a
Juan has the earliest class (1a).
Here is another example:
Please enter three students' names and hit enter after each:
Alicia
Sven
Ming
Please enter their earliest classtimes and hit enter after each:
2a
5a
4a
Alicia has the earliest class (2a).
Update: adding the example below for giving names to parts of a tuple
You can also give separate names to the parts of a tuple:
animalName, nameLength = b # ('cat', 3), so animalName = 'cat' and nameLength = 3
print("Animal:", animalName)
print("Length:", nameLength)
Part 3: Reflection
# You should be equipped to complete this part after Lesson 1 (Monday Mar. 25).
Were there any particular issues or challenges you dealt with in completing this assignment? How long did you spend on this assignment?
Write a brief discussion (a sentence or two is fine) at the bottom of your intro.py
file (in comments, so each line should start with #
).
Here are some examples:
##### Reflection #####
# I had trouble getting the spaces right in the output.
# I had to look up how to use print() in the Python documentation.
# I spent 4 hours on this assignment.
##### Reflection #####
# I started late, so I had to rush to make sure I knew how to use VS Code.
# It may be good to start early next time.
# I spent 3 hours on this assignment.
##### Reflection #####
# It went fine; I found what I needed in my notes.
# I spent 1 hour on this assignment.
Grading
This assignment will be graded out of 100 points, as follows:
- 10 points - submit a valid
.py
file with the right name (intro.py
) - 10 points -
intro.py
file contains top-level comments with author name, collab. statement (Part 0) - 20 points - prompt for user first and last name, then print message with them (Part 1)
- 20 points - prompt for user age in years, then print message with age in days (Part 1)
- 15 points - prompt for three student-class pairs, then print the earliest time and person (Part 2)
- 5 points - did not use any
if
statements (Part 2) - 10 points - match example interactions exactly (e.g., punctuation, spaces, etc.; Parts 1+2)
- 10 points - reflection in comments at the bottom of the
intro.py
file (Part 3)
What you should submit
You should submit a single intro.py file on Moodle at this link.