The due date for this homework is Thursday, Sep 12 at 11:59PM.
This assignment is designed to give you a first introduction to writing programs in Python! By completing this assignment, you will demonstrate that you understand a number of important concepts:
print()
,
input()
, and round()
Remember, computer science is a science. Always write code with a prediction in mind. While you can sparingly code to try and see output, you should focus on thinking through what you want your code to do, what you expect as a result, and compare what your code actually does to your result.
Before you type, trace the execution of the starter code already provided
Sometimes sub-tasks go together, like computation followed by printing. You should consider them simultaneously if that helps you follow the correctness of your work.
Use extra steps if needed for your thought process, printing results to test your code. Just remember to remove these extra prints once you know your code is correct.
Match your output to the output given. Precision is important in computer science.
Don’t change file names. You’re generally given .py
files to work in. Don’t change the filenames of those files.
Your task is to complete following steps:
hw1_types.py
and
hw1_madlibs.py
, in the unzipped folder. You are expected to
write your programs in these files.hw1_types.py
. This file is used in Task 1.hw1_madlibs.py
. This file is used in Task 2.trip_plan
. This file is from Task 3.Notice that each starter .py
file has a header with some
information for you to fill in. Please do so. Your feedback helps the
instructors better understand your experiences doing the homeworks and
where we can provide better assistance.
When we are assessing your code, higher levels of achievement are demonstrated, in part, by
In the hw1_types.py
file, you will find that the
main
function contains several variables that are already
defined. This is starter code, which is provided to assist you
for the relevant task. Starter code will be denoted by comments and
instructions as to whether the provided code should be modified or not.
For this task (and all those in this homework), do not modify the
starter code. You should write your code below the starter code (as
indicated in the file).
Your task is to:
Write code to print
the type of each of the existing
variables in the same order in which they were assigned.
Write code convert the value of r
to a float, raise
it to the power of u
, and display the type of the result,
without modifying the values of any of the variables.
Update the variable q
to be the remainder when
t
is divided by u
.
Print the new value of the variable q
.
Print the value of the variable s
Write code make a new variable z
which contains the
value'hi43hi43hi43'
by only using basic operators and the
variables.
Print the value of the variable z
.
The full output of your program should be exactly equivalent to the following:
<class 'str'>
<class 'str'>
<class 'str'>
<class 'int'>
<class 'int'>
<class 'float'>
1
hi
hi43hi43hi43
In the hw1_madlibs.py
file, write a short program that
asks for a noun (string) and two verbs (both strings). (The verbs should
be third person singular). Using the three inputs, your program should
print the sentence:
If it <verb 1> like a <noun> and <verb 2> like a <noun>, it probably is a <noun>.
Here is an example run of the program:
What's the noun? cat
What's the first verb? sleeps
What's the second verb? eats
If it sleeps like a cat and eats like a cat, it probably is a cat.
Here’s another example:
What's the noun? river
What's the first verb? flows
What's the second verb? bubbles
If it flows like a river and bubbles like a river, it probably is a river.
The semester has just started and you are already tired of the dinning hall food. You and your friends would like to know how much time it takes to drive to a grocery store. Oddly, you can only measure distance in feet.
In this task, you should sketch out either by hand on paper or on a whiteboard (make sure to take a picture) an algorithm that helps you plan your trip. Your algorithm should ask for a driver’s name, the distance (in feet), and the speed (in miles per hour). It should calculate and report how long it will take for the driver to arrive at the destination. The time should be reported in units of days, hours, minutes, and seconds (rounding to the nearest second).
Your sketch should not be python code. Rather, it should describe the logic and steps needed to accomplish the task, drawing on the computational thinking you have demonstrated in lab and in class.
The following are some examples to demonstrate what your algorithm should output.
Fig
is hungry. Price Chopper is roughly
14,446 feet
from Colgate. You are driving in the town, so
your average speed will be 31 mph
. Your program’s output
should look like this:
Who will be driving? Fig
How far away is the destination (in feet)? 14446
How fast will fig be driving on average (in mph)? 31
It will take fig 0 days 0 hours 5 minutes and 18 seconds.
Luiz
is willing to drive you all to Asia Food Market.
The distance between Colgate and Asia Food Market is roughly
204,431 feet
and Luiz
will be driving at
30 mph
(he is new to driving and nervous). Your program’s
output should look like this:
Who will be driving? Luiz
How far away is the destination (in feet)? 204431
How fast will Luiz be driving on average (in mph)? 30
It will take Luiz 0 days 1 hours 17 minutes and 26 seconds.
Aditi
will be driving you all to Pike Place Fish Market
in Seattle. The distance is roughly 14,691,600 feet
, and
Aditi
will be driving at 75 mph
on average.
Your program’s output should look like this:
Who will be driving? Aditi
How far away is the destination (in feet)? 14691600
How fast will Aditi be driving on average (in mph)? 75
It will take Aditi 1 days 13 hours 6 minutes and 0 seconds.
Submit two Python files and your trip_plan
to the
platform indicated in your class section:
hw1_types.py
hw1_madlibs.py
trip_plan
(either a scanned pdf of your algorithm or a
photo)Remember to complete the questions at the top of each file before submitting.