Lab 4: Advanced Conditional Problems, String Operations and Methods

Goals

After the lab, you should be proficient at

  1. solving advanced conditional problems
  2. solving basic text problems
  3. format output nicely in Python

Linux

As usual, create a directory for the programs and output you develop in this lab.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab4.1.py through lab4.7.py.

Your programs will be graded on correctness, efficiency, style, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.

After you've developed a correct solution to each program, close reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab4.x.out, where x is the problem number.

  1. (10) Write a program that takes a number as input and prints ``Eureka!'' if the number is between 500 and 1000, inclusive; otherwise, print ``Your number (<the number>) is out of range.'' (Fill in the user's number for <the number>.)
  2. (5) Copy the previous problem and add only a not and a pair of parentheses to reverse the behavior of your program.
  3. (10) Write a program that asks the user to enter two values: the width and height of a box. Your program should then draw a box of dots (periods) in the terminal window with the given width and height.

    Example Output:

    This program draws a box.
    
    Enter width (2-80): 15
    Enter height (2-20): 5
    
    ...............
    .             .
    .             .
    .             .
    ...............
    
  4. (10 pts) Using a for loop, draw a diagonal line that looks like:
    \
     \
      \
       \
        \
    

    Think about the pattern of what is getting printed. (Hint: a string operator may be useful.)

    After you have that working, have the user enter the size of the diagonal line and draw a line of the appropriate size.

  5. (15 pts) Write a program that reads in a string of text and outputs the text as follows:
    Enter the text: we love CS
    w
    we
    we 
    we l
    we lo
    we lov
    we love
    we love 
    we love C
    we love CS
    we love C
    we love 
    we love
    we lov
    we lo
    we l
    we 
    we
    w
    
  6. (15 pts) Write a program that creates a table of Olympic competition running distances in meters, kilometers, yards, and miles. The following distances should be used: 100 m, 200 m, 400 m, and 800 m.

    Note that 1 m = .001 km = 1.094 yds = .0006215 mi

    Calculate and display the results, formatted in the following manner:

    
    Meters   Kilometers     Yards    Miles
    ---------------------------------------
       100        0.100     109.4    0.062
       200        -----     -----    -----
       400        -----     -----    -----
       800        -----     -----    -----
      1600        -----     -----    -----
    

    Note: Make sure your output looks exactly as above, but you will have all the converted values filled in.

Finishing up: What to turn in for this lab

  1. IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
  2. Copy your lab4 directory into your turnin directory.
  3. Create the document to print, using the printLab.sh command.
  4. View the file you just created using the gv command to ensure that you don't have any "junk" in the output.

    Again, you should probably print from the labs directory.

    Print the file using the lpr command introduced in the first lab.

Labs are due at the end of lab. You should hand in the printed copy at the beginning of class, and the electronic version should be in the turnin directory before the end of lab.

Grading (65 pts)