MONT 112G, Fall 2011

    Home | | Course Schedule | | Assignments | | Lecture Notes

    Laboratory 2
    Due at the end of today's class

    Problem #1.

    The following Python program will input a string and print out the first letter of the word.

      word = raw_input("Please enter a word: ")
      print "The first letter of the word is " + word[0]

    A sample session might look like the following:

      Please enter a word: friends
      The first letter of the word is f

    Note: User input is underlined.

    Notice that when the user does not enter anything, but hits the return key immediately after the prompt, the program above generates an error. (Try it!)

    Write a program, named wordCheck.py, that uses a conditional to test whether the length of the word entered is zero. (Recall that we can find the length of a string, using the len( ) command. len(word) gives the length of the string that is stored in the variable, word). If the length of the word entered is equal to zero, the program should print out:

      No characters were entered.

    Otherwise, it should print out the first letter of the word as in the above example.

    A sample session in which the user did not enter any characters might look like the following:

      Please enter a word:
      No characters were entered.

    If the user enters a word of non-zero length, the program output should look like that shown in the example above.


    Problem #2.
    Write a Python program, called square.py, that asks the user for a size and uses a turtle to draw a square with a side length of that size. To use a turtle, first import the turtle class using the line:

      from turtle import Pen as Turtle
    This should be the first line of your program (after your program prologue comments with your name, date, assignment, class and purpose of the program).

    Next you should prompt for and read in the size of the square.

    Next, create a turtle object using the statement:

      yertle = Turtle( )
    Use the forward(distance) and right(angle) turtle methods to draw a square. For example, to turn the turtle right by 90 degrees, use the statement:
      yertle.right(90)
    You should make your turtle end up in its original starting position and facing the same way as it started.


    Problem #3.
    Write a Python program, named drawingChoice.py, that asks the user if they want to draw an X or an O.

    If the user enters "X", your program should use a turtle object to draw an X.

    If the user enters "O", your program should use a turtle object to draw an O.

    If the user enters anything else, your program should print:

      That is not what I asked for!
    The following decision tree diagrams how the program should run:

    The following Python code will draw an X:

    
        yertle.up( )
        yertle.goto(-50, 50)
        yertle.down( )
        yertle.right(45)
        yertle.forward(141)
        yertle.up( )
        yertle.goto(-50, -50)
        yertle.down( )
        yertle.left(90)
        yertle.forward(141)
    
    
    The following Python code will draw an O:
    
        yertle.up( )
        yertle.goto(0, -50)
        yertle.down( )
        yertle.circle(50)
    
    
    Use a conditional to determine what to draw.

    What To Turn In.
    A printed listing of each lab program. Be sure that your name is in the program prologue's comment section of EACH program.

    Reminder.
    Be sure to save a copy of each program. You can email the .py files to yourself on your gmail account
    You are responsible for keeping a copy of every program until the graded assignment is handed back.