Name:
Answer Key

SEE BODY TAG

SEE BODY TAG

 

SEE BODY TAG


Write scripts for the following questions, and upload them to BOLT.
Begin each script with a comment that includes your name, the date, and the question number.

  1. [15 pts] Name this script "q:arithmetic".

    Write a Python3 script that follows the "IPO" pattern for a program. It must prompt for two numbers from the user, then perform some arithmetic on the numbers and print out the results. The arithmetic consists of each of these operations:

    • addition — first_number + second_number
    • subtraction — first_number - second_number
    • multiplication — first_number * second_number
    • division — first_number / second_number
    • exponentiation — first_number ** second_number

    Here are a couple of example runs (run from command line, or with Spyder):

    $ python3 q:arithmetic.py
    First number? 3
    Second number? -5
    3.0 + -5.0 = -2.0
    3.0 - -5.0 = 8.0
    3.0 * -5.0 = -15.0
    3.0 / -5.0 = -0.6
    3.0 ** -5.0 = 0.00411522633744856
    $
    $ python3 q:arithmetic.py
    First number? 7.1
    Second number? 10
    7.1 + 10.0 = 17.1
    7.1 - 10.0 = -2.9000000000000004
    7.1 * 10.0 = 71.0
    7.1 / 10.0 = 0.71
    7.1 ** 10.0 = 325524355.100988
    $
    
        
     
  2. Name this script "q:loop".

    Write a Python3 script that uses a loop to produce a one-dimensional table. It must get two inputs from the user; a floating-point number (a number with a decimal point) and an integer.

    1. [10 pts] Prompt the user for each input. You may assume that the integer will be a positive number.

    2. [10 pts] Write a loop that prints out the user's floating-point number, raised to each power from 0 up to the user's integer number, along with the (integer) power itself.

    Here are a couple of example runs (run from command line, or with Spyder):

    $ python3 q:loop.py
    base? 3.6
    max. power? 5
    0 1.0
    1 3.6
    2 12.96
    3 46.656000000000006
    4 167.9616
    5 604.6617600000001
    $
    $ python3 q:loop.py
    base? -0.9
    max. power? 4
    0 1.0
    1 -0.9
    2 0.81
    3 -0.7290000000000001
    4 0.6561
    $
    
        
     
  3. Name this script "q:table".

    Write a Python3 script that uses nested loops to produce a two-dimensional table. This table is similar to a multiplication table, but it is a division table.

    1. [5 pts] Prompt the user for an X value and a Y value.

    2. [5 pts] Write nested loop thats that print one line for each Y value from 1 up to (but not including) the user's input. On each line, write one output-value for each X value from 1 up to (but not including) the user's input. The output-value must be the (x / y).

    3. [5 pts] Print a "header row" that labels each column, and a "header column" that labels each row.

    Here are a couple of example runs (run from command line, or with Spyder):

    $ python3 q:table.py
    number of rows? 4
    number of columns? 6
      :  1    2    3    4    5
    1 :  1.0  2.0  3.0  4.0  5.0
    2 :  0.5  1.0  1.5  2.0  2.5
    3 :  0.3333333333333333  0.6666666666666666  1.0  1.3333333333333333  1.6666666666666667
    $
    $ python3 q:table.py
    number of rows? 6
    number of columns? 4
      :  1    2    3
    1 :  1.0  2.0  3.0
    2 :  0.5  1.0  1.5
    3 :  0.3333333333333333  0.6666666666666666  1.0
    4 :  0.25  0.5  0.75
    5 :  0.2  0.4  0.6
    $
    
        
     
  4. Name this script "q:list".

    Write a Python3 script that builds and works with a list of colors.

    color
    "red"
    "orange"
    "yellow"
    "green"
    "blue"
    "indigo"
    "violet"
    1. [5 pts] Your program must first create a list containing the colors in the table. Then print the length of the list (use a function, don't count the list by hand).

    2. [5 pts] Prompt for and input an "index" number from the keyboard, then print that element of the list. If the user enters an invalid (too big) number, print an error message instead.

    3. [5 pts] Print the slice of the list that starts with "Yellow" and ends with "Indigo".

    4. [5 pts] Prompt for and input a color from the keyboard. Use the .index() method to print the index of the entered color within the list.

    5. [5 pts] Concatenate the first element and the last element into a new list, and print that out.

        
     
  5. Name this script "q:dict".

    Write a Python3 script that builds and works with a dictionary of colors and wavelengths.

    wavelength color
    450 "Blue"
    550 "Green"
    425 "Indigo"
    800 "Infra-red"
    600 "Orange"
    650 "Red"
    300 "Ultra-violet"
    400 "Violet"
    580 "Yellow"
    1. [5 pts] First create a dictionary whose keys are the wavelengths, and whose values are the color names, from the table at right. Then print the keys of the dictionary.

    2. [5 pts] Print out each value of the dictionary, one color per line.

    3. [5 pts] Write statements that add two additional colors and wavelengths to the dictionary: "Infra-red", wavelength 800; and "Ultra-violet", wavelength 300.

    4. [5 pts] Print out the dictionary, sorted by wavelength (key), one wavelength and color per line.

    5. [5 pts] Ask the user for a wavelength number. If the number is one of the dictionary's keys, print out the corresponding color name. Otherwise print out the two colors that it is "between". (If it is outside the range of colors, print an error message.)

        
     
  6. [25 pts] Name this script "q:phrase".

    Write a Python3 script that can be run from the command line. The script must input two phrases. It must then print back the length of each phrase followed by that phrase, and then the average length of the phrase.

    The average length is simply the sum of the two phrase lengths, divided by 2.

    Here are a couple of example runs:

    $ python3 question2.py
    First phrase? hello
    Second phrase? goodbye, Mr. Chips!
      5   hello
     19   goodbye, Mr. Chips!
     12.00 average length
    $
    $ python3 question2.py
    First phrase? It was the best of times,
    Second phrase? it was the worst of times.
     25   It was the best of times,
     26   it was the worst of times.
     25.50 average length
    $
    
        
     
  7. [25 pts] Name this script "q:quadratic/span>".

    Write a Python3 script that can be run from the command line. The script must input three values, a, b, and c. Then it must calculate the roots of the Quadratic Formula, and print out the results as shown below. The output must be formatted as in the example below.

    The roots of the Quadratic Formula are given by:

        rootPlus  =  (-b + (b2 - 4×a×c)0.5) / (2×a)
        rootMinus  =  (-b - (b2 - 4×a×c)0.5) / (2×a)
    
    question 4 output