HW 1

Programming with IF and WHILE

Introduction to Programming -- CS 140
Spring, 2008

This assignment is due at midnight on Friday, February 22

 



Part one.  Debugging

We will start by working on your debugging skills. Here are 3 programs. For each of these find what is wrong and make a note about how to fix it. You don't need to hand these in..

Part two.  Your programs

  1. Write a program that reads three names and says how many of these are the same. If there are duplicates, the most common name is printed. For example, here is one run of the program; what the computer prints is in boldface:
    Enter the first name: Larry
    Enter the second name: Daryl
    Enter the third name: Daryl
    The name 'Daryl' was entered twice.

    Here is another run:
    Enter the first name: Eric
    Enter the second name: Ginger
    Enter the third name:Jack
    No name was entered more than once.

    To write this, you first need three raw_input() statements to read the three names. You then need logic section of your program where you look at all of the possible cases. If you don't think through this carefully you can tie yourself into knots. First, ask if the first name is equal to the second. If it is, then either two or three of the names will be the same; you can choose between these cases by asking if the third is the same as the first. Uf tge furst twi are not the same you will need to compare the third with each of them. Each time you know the answer have a print statement report the result.
  2. In this one we will write a simple version of the game 21. For this we will need to generate some random numbers, which is easy to do in Python. If you include the following line at the top of a program:

    from random import *

    then you can make use of the language's random number facilities. One of these is the function randint(a, b), which gives a random number between a and b. Here is a small program that uses these. This will print 20 random numbers, each between 1 and 10.

    from random import *
    def main():
         i = 1
         while i <= 20:
               print randint(1, 10)
               i = i + 1

    main()

    For this assignment you are to write a program that gives the user random numbers in steps. At each step the user is asked if she wants another number. If she does, the program adds a random number between 1 and 10 onto her total. The game ends when she quits or when her total is more than 21. The goal, of course, is to get as close to 21 as possible without exceeding it. Here is a typical run of the program:

    Your current total is 0.
    Do you want another number? yes or no: yes
    Your number is 2, making your total 2.
    Do you want another number? yes or no:
    yes
    Your number is 9, making your total 11.
    Do you want another number? yes or no:
    yes
    Your number is 10, making your total 21.
    Do you want another number? yes or no:
    no

    You need to hand in files for programs A and B.

You need to hand in files that contain programs A and B. I have set up a Blackboard site for the class. Go to the Blackboard Site for Introduction to Programming; it should appear in your "My Courses" list in Blackboard. If you click on the HW1 button you will get to a screen that lets you hand in the files. You only need to hand in solutions for these two programming projects, not for the debugging questions.