First Summer 2008


Lab Two: Getting Started with Python.
Part of the lab we will be discussing the programs developed in lecture.

But not much will be used with that.

The goal of the lab will be for you to write new programs, based on the lecture:

  1. (Calculating a Car's Autonomy) Given a car's mileage (expressed in miles per gallon) and the amount of fuel in the car's tank (expressed in gallons) write a formula to determine the autonomy of that car in miles. Then use the formula that you wrote to write a program that asks the user for the mileage of a car and the amount of fuel in the car and prints back the autonomy of the car in miles. When finished your program should behave like this:

    >>>
    What is the car's efficiency (miles/gallon)? 20
    How many gallons of fuel do you have? 3.5
    Your car's autonomy is: 70.0 miles.
    
    >>> 

  2. (An Investment Problem) Assume that your bank account starts with a certain amount of dollars in it and you never take money out. At the end of each year the amount in your account goes up with 5%. Write a program that asks the user for the initial amount of money in the account, then shows what happens to the account over a period of ten years. What would you have to do to run the analysis over a longer period of, say, 20 years? Here's how your program might behave when you have it done:

    >>>
    What is amount of money? 100
    After one year the amount becomes 105.0
    After two years the amount becomes 110.25
    After three years the amount becomes 115.7625
    After four years the amount becomes 121.550625
    After five years the amount becomes 127.62815625
    After six years the amount becomes 134.009564062
    After seven years the amount becomes 140.710042266
    After eight years the amount becomes 147.745544379
    After nine years the amount becomes 155.132821598
    After ten years the amount becomes 162.889462678
    
    >>>
    What is amount of money? 5000
    After one year the amount becomes 5250.0
    After two years the amount becomes 5512.5
    After three years the amount becomes 5788.125
    After four years the amount becomes 6077.53125
    After five years the amount becomes 6381.4078125
    After six years the amount becomes 6700.47820312
    After seven years the amount becomes 7035.50211328
    After eight years the amount becomes 7387.27721895
    After nine years the amount becomes 7756.64107989
    After ten years the amount becomes 8144.47313389

  3. (Averages) Write a program that asks the user to enter a number per line and prints back (after every line) the current average of all the numbers entered up to that point. Program stops after the sixth time a number is entered and the average reported. Here's how your program might behave when finished:

    >>>
    Enter number: 3
    The average so far is: 3.0 / 1  =  3.0
    Enter number: 2
    The average so far is: 5.0 / 2  =  2.5
    Enter number: 3
    The average so far is: 8.0 / 3  =  2.66666666667
    Enter number: 4
    The average so far is: 12.0 / 4  =  3.0
    Enter number: 3
    The average so far is: 15.0 / 5  =  3.0
    Enter number: 5
    The average so far is: 20.0 / 6  =  3.33333333333
    Thanks for using this program.
    
    >>> 

    Note: in your program you must use only two variables at most.

  4. (Parens) Write a program that receives a string and rewrites it with parens around each vowel. Here's how my program works:

    >>>
    Enter sentence: there is a tomato in every automaton
    th(e)r(e) (i)s (a) t(o)m(a)t(o) (i)n (e)v(e)ry (a)(u)t(o)m(a)t(o)n
    
    >>> 

  5. (Stenographer) Write a program that removes all vowels from a string of characters. Essentially the program asks the user for a line of input, then writes back only the consonants in the line, Here's how your program might look when finished.

    >>>
    Enter sentence: there is a tomato in every automaton
    thr s  tmt n vry tmtn
    
    >>> 

So today in lab we will work on these programs.


Last updated: May 7, 2008 by Adrian German for A201/A597