Lab Exercises:

You can complete these problems alone or with a partner, in any order you like. Or if you prefer, you may write any other Python program that interests you. Please ask Marty if you need any help!


Day 1:

  1. Write a program that asks the user about textbook prices and reports how overpriced the textbooks are. (You may wish to read this number from the user with the input command. You can round numbers with the round command.) You should match the following output

    How much do you want to pay for each textbook? 50
    How much did the average textbook actually cost? 80
    How many books did you buy? 5
    Each book is overpriced by $30 ( 60% )
    You got ripped off by $150 total

    You may assume that the user enters a positive integer for each input value above.


  2. If you ever find yourself buying a house, you'll want to know what your monthly payment for the loan is going to be. Write a complete program that asks for information about a loan and prints the monthly payment.

    The formula for computing monthly mortgage payments involves the loan amount, the total number of months involved (a value we call n) and the monthly interest rate (a value we call c). The payment formula is given by the following equation:

    An example run of your program might produce the following output (user input is underlined):

    This program computes monthly loan payments.
    Loan amount? 275000
    Number of years? 30
    Interest rate? 6.75
    Your payment is $1783


    Note that the numbers being read as input don't match the way they should be used in the formula. The term of the loan is read as years instead of months. The interest rate percentage is being read in as a yearly rate instead of a monthly rate and as a whole number rather than a true percentage, so the value 6.75 should actually become 0.005625 by dividing it by 12 and by 100. You will have to make these conversions as part of your program.

  3. Write a program that counts a number's factors and determines whether the number is prime.

    What is your favorite number? 24
    24 has 8 factors
    24 is not prime
    What is your favorite number? 31
    31 has 2 factors
    31 is prime

    Hint: To count the factors of some integer n, use a loop that tests every integer less than n to see whether n is divisible by it. (How do you know whether one integer is divisible by another?)


Day 2:

  1. You may not know that credit card numbers contain several pieces of information for performing validity tests. For example, Visa card numbers always begin with 4, and a valid Visa card number also passes a digit-sum test known as the Luhn checksum algorithm. Luhn's algorithm states that if you sum the digits of the number in a certain way, the total sum must be a multiple of 10 for a valid Visa number. Systems that accept credit cards perform a Luhn test before contacting the credit card company for final verification. This lets the company block fake or mistyped credit card numbers.

    The algorithm for summing the digits is the following. Consider each digit of the credit card to have a zero-based index: the first is at index 0, and the last is at index 15. Start from the rightmost digit and process each digit one at a time. For digits at even-numbered indexes (the 14th digit, 12th digit, etc.), simply add that digit to the cumulative sum. For digits at odd-numbered indexes (the 15th, 13th, etc), double the digit's value, then if that doubled value is less than 10, add it to the sum. If the doubled number is 10 or greater, add each of its digits separately into the sum.

    The following pseudocode describes the Luhn algorithm to sum the digits:

    sum = 0.
    for each digit of credit card number, starting from right,
        if the digit's index is even,
            add the digit to sum.
            double the digit's value.
            if the doubled value is less than 10,
                add the doubled value to sum.
                split the doubled value into its two digits.
                add the first digit to sum.
                add the second digit to sum.

    4111111111111111 and 4408041254369873 are example credit card numbers that pass the Luhn algorithm. The following figure shows the algorithm summing the latter number in detail. Notice how digits at even indexes are doubled and potentially split into two digits if they exceed 10 when doubled. For example, the number 7 at index 8 which is doubled to 14 which split to make 1+4.

    An example checksum using the Luhn algorithm.
    CC #    4408 0412 5436 9873
            4   4   0   8   0   4   1   2    7    4   3   6    9    8    5    3
    Scale  *2      *2      *2      *2       *2       *2       *2        *2 
            8   4   0   8   0   4   2   2   14    4   6   6   18    8   10    3
    Sum   = 8 + 4 + 0 + 8 + 0 + 4 + 2 + 2 + 1+4 + 4 + 6 + 6 + 1+8 + 8 + 1+0 + 3
          = 70
    70 is divisible by 10, therefore this card number is valid.

    Write a program where the user can type in a credit card number and receive a message stating whether the number was valid.


  2. Write a program that draws the following figure:


  3. Write a program that draws the following figure:

    Part of the challenge is using loops to reduce the redundancy of the figure.


  4. Write a program that draws the following figure:

    Part of the challenge is using loops to reduce the redundancy of the figure.