In the “Taste of Python” lesson this week we have learned a few basic things in Python. Now you get your chance to demonstrate your skills.
This exercise is inspired by an analysis that determined Finland is the world's happiest country. As the instructors of the Geo-Python course, we think we've found a better way to assess the individual happiness of people in Finland (particularly those taking this course!), and we're going to test our idea with you. In five easy steps (or problems) we'll be able to reveal your happiness based on two of life's most important happiness factors: Ice cream and sleep!
Open Jupyter lab from the Exercise 1 landing page on the course site, and create a new notebook.
In the top cell of the new notebook:
-
Create a variable called
ice_cream_rating
and use it to store an integer value (whole number) between 0 and 10 that reflects your general opinion about how much you enjoy eating ice cream. -
Create another variable called
sleeping_rating
and use it to store another integer between 0 and 10 that reflects your opinion about how much you enjoy sleeping. -
Run the cell by pressing Shift-Enter.
In the new cell that appears:
-
Use the
input()
function to prompt the user to enter their first name and store it in a variable calledfirst_name
. -
Use the
input()
function again to prompt the user to enter their last name and store it in a variable calledlast_name
. -
(Optional) Define a third variable called
my_name
that will combine the user's first and last names into a single character string with a space between the names. -
Run the cell and enter the requested information at the prompts.
In order to assess happiness it is necessary to combine the ice cream and sleeping ratings. To do this:
-
Calculate the average of your
ice_cream_rating
andsleeping_rating
variables and store the resulting value in a variable calledhappiness_rating
. -
Run the cell by pressing Shift-Enter.
The next step is to investigate the kinds of data we're working with. In another Python cell:
-
Use a built-in Python function to check the data types of the
ice_cream_rating
,first_name
, andhappiness_rating
variables. In order to see the data types for more than one variable in a single Python cell, you will need to print out these values using another built-in Python function. -
Run the cell by pressing Shift-Enter.
-
Did all of the data types make sense? Were there any data types that were different than you expected? In the new Python cell that appeared after running the code above, enter your responses to the questions above.
-
For the cell containing your answers to the questions above, change the kind of cell from Code to Markdown.
-
Run the Markdown cell by pressing Shift-Enter.
Finally, you can use your Python skills to generate output on the screen similar to that below. Use one command to generate each line of output.
My name is Dave and I give eating ice cream a score of 9 out of 10!
I am Dave Whipp and my sleeping enjoyment rating is 8 / 10!
Based on the factors above, my happiness rating is 8.5 out of 10, or 85.0 %!
Note that your code should replace “Dave” with the contents of your first_name
variable, “9” with your value stored in the variable ice_cream_rating
, etc. For your equivalent text with the full name (e.g., "Dave Whipp"), you can either use the values from the variables first_name
and last_name
, or the value in variable my_name
.
To complete this exercise, upload your changes to GitHub in your own Exercise 1 repository that you created using the GitHub Classroom link.