You are on page 1of 4

CS177 LAB 2

Before the Lab
Study Chapter 1 and Chapter 2 of the textbook.

Setting up your Environment
Go to your working directory in “data.cs.purdue.edu” and create a directory “cs177/lab2”.
Refer to the previous lab if you need to remember the steps to do so. Then start the IDLE
Python Interpreter.

Exercise 1
The following code shows a program that is used to convert Celsius to Fahrenheit.

# celsiusToFahrenheit.py
# A program to convert Celsius temps to Fahrenheit
# by: Susan Computewell

def main():
    celsius = eval(input("What is the Celsius temperature?
"))
    fahrenheit = (9/5) * celsius + 32
    print("The temperature is ",fahrenheit," degrees
Fahrenheit.")

main()

Write a program that converts inches to centimeters using the formula:

cm = in * 2.54
The program should ask for the number of inches and it should print the distance in centimeters
as follows:

Example:
What is the distance in inches? 10
10 inches is 25.4cm.

Save the program as inchesToCentimeters.py in your lab2 directory. Make sure that it runs
correctly.
Exercise 2

The following program counts from 0 to 9.

#count.py
# A program to count from 0 to 9
# by: Susan Computewell

def main():
    for i in range(10):
        print(i)

main()

 Write a program that displays the times table of a given number as follows:

Example:
Times table for what number? 5
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50

Notice that the table goes from 1 to 10. Hint: Use (i+1) instead of in the print statement.

Save the program as mul.py in your lab2 directory. Make sure that it runs correctly.
Exercise 3

The following program shows the amount of money you will have at the end of every month if
you save $10dls every month for 18 months in your piggy­bank. This assumes that you have an
initial amount of $20dls.

#piggybank.py
# A program that computes the amount of money in
# your piggybank
# by: Susan Computewell
def main():
    initialAmount = 20
    amountPerMonth = 10
    numberOfMonths = 18

    amount = initialAmount
    for month in range(numberOfMonths+1):
        print("month: ", month, " amount: $", amount)
        amount = amount + amountPerMonth

main()

Based on the previous program, write a program that will show the amount that you will have in
a bank that offers annual interest in your deposits. The program will ask the initial deposit, the
interest of the bank, and the number of years. Then it will print the amount after each year.

Example Output:
Initial Amount? 100
%Interest? 2
Number of Years? 10
Year  0  amount: $ 100
Year  1  amount: $ 102.0
Year  2  amount: $ 104.04
Year  3  amount: $ 106.1208
Year  4  amount: $ 108.243216
Year  5  amount: $ 110.40808032
Year  6  amount: $ 112.616241926
Year  7  amount: $ 114.868566765
Year  8  amount: $ 117.1659381
Year  9  amount: $ 119.509256862
Year  10  amount: $ 121.899441999

Name the program savings.py and save it in cs177/lab2.
Turnin your Lab
Run putty and login to data.cs.purdue.edu. Turnin your lab by typing:

cd cs177
turnin –c cs177 –p lab2 lab2

Grade Allocation

Max Current
Exercise 1 30
Exercise 2 30
Exercise 3 40

You might also like