::: {#navbar .navbar .navbar-default .navbar-fixed-top role="navigation"}
::: {.container}
::: {.navbar-header}
[]{.icon-bar} []{.icon-bar} []{.icon-bar}

<div>

[![](../_static/img/RAIcon.png)](/runestone/default/user/login){.brand-logo}
[fopp](../index.html){#rs-book-toc-link .navbar-brand}

</div>
:::

::: {.navbar-collapse .collapse .navbar-ex1-collapse}
-   
-   []{#ad_warning}
-   []{#browsing_warning}
-   
-   [*[Search]{.visuallyhidden
    aria-label="Search"}*](#){.dropdown-toggle}
    -   [Table of Contents](../index.html)

    -   [Book Index](../genindex.html)

    -   

    -   ::: {.input-group}
        :::
-   
-   [*[User]{.visuallyhidden aria-label="User"}*](#){.dropdown-toggle}
    -   []{.loggedinuser}

    -   

    -   [Course Home](/ns/course/index)

    -   [Assignments](/assignment/student/chooseAssignment)

    -   [Practice](/runestone/assignments/practice)

    -   [[Peer Instruction
        (Instructor)](/runestone/peer/instructor.html)]{#inst_peer_link}

    -   [Peer Instruction (Student)](/runestone/peer/student.html)

    -   

    -   [Change Course](/runestone/default/courses)

    -   

    -   [[Instructor\'s
        Page](/runestone/admin/index)]{#ip_dropdown_link}

    -   [Progress Page](/runestone/dashboard/studentreport)

    -   

    -   [Edit Profile](/runestone/default/user/profile){#profilelink}

    -   [Change
        Password](/runestone/default/user/change_password){#passwordlink}

    -   [Register](/runestone/default/user/register){#registerlink}

    -   [Login](#)

    -   ::: {.slider .round}
        :::

        Dark Mode
-   
-   [[*[Scratch Activecode]{.visuallyhidden
    aria-label="Scratch Activecode"}*](javascript:runestoneComponents.popupScratchAC())]{#scratch_ac_link}
-   
-   [*[Help]{.visuallyhidden aria-label="Help"}*](#){.dropdown-toggle}
    -   [FAQ](http://runestoneinteractive.org/pages/faq.html)
    -   [Instructors Guide](https://guide.runestone.academy)
    -   
    -   [About Runestone](http://runestoneinteractive.org)
    -   [Report A
        Problem](/runestone/default/reportabug?course=fopp&page=Exercises)
-   

```{=html}
<!-- -->
```
-   
-   [This Chapter](../index.html){.dropdown-toggle}
    -   [2.1
        Introduction](intro-VariablesExpressionsandStatements.html){.reference
        .internal}
    -   [2.2 Values and Data Types](Values.html){.reference .internal}
    -   [2.3 Operators and Operands](Operators.html){.reference
        .internal}
    -   [2.4 Function Calls](FunctionCalls.html){.reference .internal}
    -   [2.5 Data Types](DataTypes.html){.reference .internal}
    -   [2.6 Type conversion
        functions](ConvertTypeFunctions.html){.reference .internal}
    -   [2.7 Variables](Variables.html){.reference .internal}
    -   [2.8 Variable Names and
        Keywords](VariableNamesandKeywords.html){.reference .internal}
    -   [2.9 👩‍💻 Choosing the Right Variable
        Name](WPChoosingtheRightVariableName.html){.reference .internal}
    -   [2.10 Statements and
        Expressions](StatementsandExpressions.html){.reference
        .internal}
    -   [2.11 Order of Operations](OrderofOperations.html){.reference
        .internal}
    -   [2.12 Reassignment](Reassignment.html){.reference .internal}
    -   [2.13 Updating Variables](UpdatingVariables.html){.reference
        .internal}
    -   [2.14 👩‍💻 Hard-Coding](HardCoding.html){.reference .internal}
    -   [2.15 Input](Input.html){.reference .internal}
    -   [2.16 Glossary](Glossary.html){.reference .internal}
    -   [2.17 Exercises](Exercises.html){.reference .internal}
    -   [2.18 Chapter Assessment](week1a2.html){.reference .internal}
-   
-   
:::
:::
:::

::: {#continue-reading .container}
:::

::: {#main-content .container role="main"}
::: {#exercises .section}
[2.17. ]{.section-number}Exercises[¶](#exercises "Permalink to this heading"){.headerlink}
==========================================================================================

::: {#spd_ex_1 .full-width .container .question component="question" question_label="2.17.1"}
1.  ::: {#q1 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ch02_ex1 component="activecode" question_label="2.17.2"}
    ::: {#ch02_ex1_question .ac_question}
    Evaluate the following numerical expressions in your head, then use
    the active code window to check your results:

    1.  `5 ** 2`{.docutils .literal .notranslate}

    2.  `9 * 5`{.docutils .literal .notranslate}

    3.  `15 / 12`{.docutils .literal .notranslate}

    4.  `12 / 15`{.docutils .literal .notranslate}

    5.  `15 // 12`{.docutils .literal .notranslate}

    6.  `12 // 15`{.docutils .literal .notranslate}

    7.  `5 % 2`{.docutils .literal .notranslate}

    8.  `9 % 5`{.docutils .literal .notranslate}

    9.  `15 % 12`{.docutils .literal .notranslate}

    10. `12 % 15`{.docutils .literal .notranslate}

    11. `6 % 6`{.docutils .literal .notranslate}

    12. `0 % 7`{.docutils .literal .notranslate}
    :::
    :::
    :::
    :::

    ::: {component="tab" tabname="Answer"}
    1.  `5 ** 2  = 25`{.docutils .literal .notranslate}

    2.  `9 * 5 = 45`{.docutils .literal .notranslate}

    3.  `15 / 12 = 1.25`{.docutils .literal .notranslate}

    4.  `12 / 15 = 0.8`{.docutils .literal .notranslate}

    5.  `15 // 12 = 1`{.docutils .literal .notranslate}

    6.  `12 // 15 = 0`{.docutils .literal .notranslate}

    7.  `5 % 2 = 1`{.docutils .literal .notranslate}

    8.  `9 % 5 = 4`{.docutils .literal .notranslate}

    9.  `15 % 12 = 3`{.docutils .literal .notranslate}

    10. `12 % 15 = 12`{.docutils .literal .notranslate}

    11. `6 % 6 = 0`{.docutils .literal .notranslate}

    12. `0 % 7 = 0`{.docutils .literal .notranslate}
    :::

    ::: {component="tab" tabname="Discussion"}
    [Show Comments](#disqus_thread){.disqus_thread_link}
    :::
    :::
:::

::: {#spd_ex_2 .full-width .container .question component="question" question_label="2.17.3"}
2.  ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_2 component="activecode" question_label="2.17.4"}
    ::: {#ex_2_2_question .ac_question}
    What is the order of the arithmetic operations in the following
    expression. Evaluate the expression by hand and then check your
    work.
    :::
    :::
    :::
:::

::: {#spd_ex_5 .full-width .container .question component="question" question_label="2.17.5"}
3.  ::: {#q5 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_5 component="activecode" question_label="2.17.6"}
    ::: {#ex_2_5_question .ac_question}
    Challenge: Many people keep time using a 24 hour clock (11 is 11am
    and 23 is 11pm, 0 is midnight). If it is currently 13 and you set
    your alarm to go off in 50 hours, it will be 15 (3pm). Write a
    Python program to solve the general version of the above problem.
    Ask the user for the time now (in hours), and then ask for the
    number of hours to wait for the alarm. Your program should output
    what the time will be on the clock when the alarm goes off.
    :::
    :::
    :::
    :::

    ::: {component="tab" tabname="Answer"}
    ::: {.runestone .explainer .ac_section}
    ::: {#spd_q5_answer component="activecode" question_label="2.17.7"}
    ::: {#spd_q5_answer_question .ac_question}
    :::
    :::
    :::
    :::
    :::
:::

::: {#spd_ex_6 .full-width .container .question component="question" question_label="2.17.8"}
4.  ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_6 component="activecode" question_label="2.17.9"}
    ::: {#ex_2_6_question .ac_question}
    It is possible to name the days 0 thru 6 where day 0 is Sunday and
    day 6 is Saturday. If you go on a wonderful holiday leaving on day
    number 3 (a Wednesday) and you return home after 10 nights you would
    return home on a Saturday (day 6). Write a general version of the
    program which asks for the starting day number, and the length of
    your stay, and it will tell you the number of day of the week you
    will return on.
    :::
    :::
    :::
:::

::: {#spd_ex_7 .full-width .container .question component="question" question_label="2.17.10"}
5.  ::: {#q7 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    Challenge: Take the sentence: *All work and no play makes Jack a
    dull boy.* Store each word in a separate variable, then print out
    the sentence on one line using `print`{.docutils .literal
    .notranslate}.

    ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_7 component="activecode" question_label="2.17.11"}
    ::: {#ex_2_7_question .ac_question}
    :::
    :::
    :::
    :::

    ::: {component="tab" tabname="Answer"}
    ::: {.runestone .explainer .ac_section}
    ::: {#spd_q7_answer component="activecode" question_label="2.17.12"}
    ::: {#spd_q7_answer_question .ac_question}
    :::
    :::
    :::
    :::
    :::
:::

::: {#spd_ex_8 .full-width .container .question component="question" question_label="2.17.13"}
6.  ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_8 component="activecode" question_label="2.17.14"}
    ::: {#ex_2_8_question .ac_question}
    Add parentheses to the expression `6 * 1 - 2`{.docutils .literal
    .notranslate} to change its value from 4 to -6.
    :::
    :::
    :::
:::

::: {#spd_ex_9 .full-width .container .question component="question" question_label="2.17.15"}
7.  ::: {#q9 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_9 component="activecode" question_label="2.17.16"}
    ::: {#ex_2_9_question .ac_question}
    Challenge: The formula for computing the final amount if one is
    earning compound interest is given on Wikipedia as

    ![formula for compound interest](../_images/compoundInterest.png)
    Write a Python program that assigns the principal amount of 10000 to
    variable `P`{.docutils .literal .notranslate}, assign to
    `n`{.docutils .literal .notranslate} the value 12, and assign to
    `r`{.docutils .literal .notranslate} the interest rate of 8% (0.08).
    Then have the program prompt the user for the number of years,
    `t`{.docutils .literal .notranslate}, that the money will be
    compounded for. Calculate and print the final amount after
    `t`{.docutils .literal .notranslate} years.
    :::
    :::
    :::
    :::

    ::: {component="tab" tabname="Answer"}
    ::: {.runestone .explainer .ac_section}
    ::: {#spd_q9_answer component="activecode" question_label="2.17.17"}
    ::: {#spd_q9_answer_question .ac_question}
    :::
    :::
    :::
    :::
    :::
:::

::: {#spd_ex_10 .full-width .container .question component="question" question_label="2.17.18"}
8.  ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_10 component="activecode" question_label="2.17.19"}
    ::: {#ex_2_10_question .ac_question}
    Write a program that will compute the area of a circle. Prompt the
    user to enter the radius and save it to avariable called
    `radius`{.docutils .literal .notranslate}. Print a nice message back
    to the user with the answer.
    :::
    :::
    :::
:::

::: {#spd_ex_11 .full-width .container .question component="question" question_label="2.17.20"}
9.  ::: {#q11 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_11 component="activecode" question_label="2.17.21"}
    ::: {#ex_2_11_question .ac_question}
    Challenge: Write a program that will compute the area of a
    rectangle. Prompt the user to enter the width and height of the
    rectangle and store the values in variables called `width`{.docutils
    .literal .notranslate} and `height`{.docutils .literal
    .notranslate}. Print a nice message with the answer..
    :::
    :::
    :::
    :::

    ::: {component="tab" tabname="Answer"}
    ::: {.runestone .explainer .ac_section}
    ::: {#spd_q11_answer component="activecode" question_label="2.17.22"}
    ::: {#spd_q11_answer_question .ac_question}
    :::
    :::
    :::
    :::
    :::
:::

::: {#spd_ex_12 .full-width .container .question component="question" question_label="2.17.23"}
10. ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_12 component="activecode" question_label="2.17.24"}
    ::: {#ex_2_12_question .ac_question}
    Write a program that will compute MPG for a car. Prompt the user to
    enter the number of miles driven and store it in a variable called
    miles and the number of gallons used stored in a variable gallons.
    Print a nice message with the answer.
    :::
    :::
    :::
:::

::: {#spd_ex_13 .full-width .container .question component="question" question_label="2.17.25"}
11. ::: {#q13 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_13 component="activecode" question_label="2.17.26"}
    ::: {#ex_2_13_question .ac_question}
    Challenge: Write a program that will convert degrees celsius to
    degrees fahrenheit.
    :::
    :::
    :::
    :::

    ::: {component="tab" tabname="Answer"}
    ::: {.runestone .explainer .ac_section}
    ::: {#spd_q13_answer component="activecode" question_label="2.17.27"}
    ::: {#spd_q13_answer_question .ac_question}
    :::
    :::
    :::
    :::

    ::: {component="tab" tabname="Discussion"}
    [Show Comments](#disqus_thread){.disqus_thread_link}
    :::
    :::
:::

::: {#spd_ex_14 .full-width .container .question component="question" question_label="2.17.28"}
12. ::: {.runestone .explainer .ac_section}
    ::: {#ex_2_14 component="activecode" question_label="2.17.29"}
    ::: {#ex_2_14_question .ac_question}
    Ask the user for the temperature in Fahrenheit and store it in a
    variable call `deg_f`{.docutils .literal .notranslate}. Calculate
    the equivalent temperature in degrees Celsius and store it in
    `deg_c`{.docutils .literal .notranslate}. Output a message to the
    user giving the temperature in Celsius.
    :::
    :::
    :::
:::

::: {#spd_ex_15 .full-width .container .question component="question" question_label="2.17.30"}
13. ::: {.runestone .parsons-container}
    ::: {#pp_2_15 .parsons component="parsons"}
    ::: {.parsons_question .parsons-text}
    Piece together the code so that a user is asked for two numbers, and
    then the sum of those two numbers is printed out.
    :::

    ``` {.parsonsblocks question_label="2.17.31" noindent="true" style="visibility: hidden;"}
            num_one = input("Please enter your first number: ")
    ---
    num_two = input("Please enter your second number: ")
    ---
    sum_of_input = int(num_one) + int(num_two)
    ---
    print(sum_of_input)
            
    ```
    :::
    :::
:::

::: {#spd_ex_16 .full-width .container .question component="question" question_label="2.17.32"}
14. ::: {.runestone .parsons-container}
    ::: {#pp_2_16 .parsons component="parsons"}
    ::: {.parsons_question .parsons-text}
    Write a program that will convert gallons to liters. This program
    will also need to get input from a user to see how many gallons
    should be converted and the result should be printed to the user.
    :::

    ``` {.parsonsblocks question_label="2.17.33" noindent="true" style="visibility: hidden;"}
            user_gallons = input("How many gallons should be converted?: ")
    ---
    num_gallons = int(user_gallons)
    ---
    liters = num_gallons * 3.785
    ---
    print("Number of liters: " + str(liters))
            
    ```
    :::
    :::
:::

::: {#spd_ex_17 .full-width .container .question component="question" question_label="2.17.34"}
15. ::: {.runestone .parsons-container}
    ::: {#pp_2_17 .parsons component="parsons"}
    ::: {.parsons_question .parsons-text}
    Write a program that will convert tablespoons to teaspoons. This
    program will also need to get input from a user to see how many
    tablespoons should be converted and the result should be printed to
    the user.
    :::

    ``` {.parsonsblocks question_label="2.17.35" noindent="true" style="visibility: hidden;"}
            user_tablespoons = float(input("How many tablespoons should be converted?: "))
    ---
    teaspoons = user_tablespoons * 3
    ---
    print("Number of teaspoons: " + str(teaspoons))
            
    ```
    :::
    :::
:::

::: {#contributed-exercises .section}
[2.17.1. ]{.section-number}Contributed Exercises[¶](#contributed-exercises "Permalink to this heading"){.headerlink}
--------------------------------------------------------------------------------------------------------------------
:::
:::

::: {style="width: 100%"}
::: {ea-publisher="runestoneacademy" ea-type="image" style="display: flex; justify-content: center"}
:::
:::

::: {#scprogresscontainer}
You have attempted []{#scprogresstotal} of []{#scprogressposs}
activities on this page

::: {#subchapterprogress aria-label="Page progress"}
:::
:::

-   [[](Glossary.html)]{#relations-prev}
-   [[](week1a2.html)]{#relations-next}
:::

::: {.container}
[]{#numuserspan}[]{.loggedinuser} \| [Back to top](#)

© Copyright 2017 bradleymiller. Created using
[Runestone](http://runestoneinteractive.org/) 7.2.10.
:::
