Lab 6 Testing via Command Line (Seconds lab)

Objectives

  • Perform integer division and modulus.

  • Display output with specific formatting.

  • Compile and run from the command line.

  • Redirect input and output using text files.

Key Terms

floor division (//)
integer operation that produces the quotient
modulus (%)
integer operation that produces the remainder

Part 1: Preparation

Develop (on paper) an algorithm for converting a total number of seconds into hours, minutes, and seconds. For example:

Input Output
Total Seconds Hours Minutes Seconds
5000 1 23 20
3625 1 0 25
1000 0 16 40

Before writing any code, think about solving this problem in general. Make sure you know how to do this task on paper! Only then can you describe the process algorithmically. Be sure to indicate the names of all variables, and list all required steps in the conversion.

Part 2: Implementation

Create a new program seconds.py in Thonny with the following structure.

  1. Write a one-sentence block comment about your assignment, along with :Author and :Version tags and Honor Code statement.
  2. Display the prompt "Enter the number of seconds: " exactly as shown. 
  3. Calculate the equivalent hours, minutes, and seconds using your algorithm from Part 1.
  4. Finally, output your result. The format must be EXACTLY as shown, including whitespace.
    (blank line)
    _____ seconds = _____ hours, _____ minutes, and _____ seconds
    (blank line)
    
    For example:
    1000 seconds = 0 hours, 16 minutes, and 40 seconds
    
  5. Test your program with several input values, and make corrections if the results are not correct.

Make sure the required number of spaces around each of the values matches the specification above. There should be exactly one space between elements on the output line, and no leading or trailing spaces. Be sure to include the right number of blank lines in the output  (you may want to use the newline character, \n). Incorrect spacing or anything other than three lines of output (each with one newline character at the end) will cause the output to be flagged as incorrect.

Part 3: Diff Testing

The requirements above specify exactly how the prompt and output should be formatted. It is easy to make small formatting errors like extra spaces, mispelled words, etc. Fortunately there are software tools that can compare the contents of two files to automate the process of checking for differences. If we have an example of the desired output we can use these tools to compare our output to the example.

Today we will use DiffMerge, a visual diff and merge tool that runs on Linux. (There are similar free tools Meld  on Linux, WinMerge on Windows or FileMerge on Mac, the latter of which comes with Xcode.)

If your program is working correctly, executing it in the terminal should look something like the following:

$ python3 seconds.py
Enter the number of seconds: 1000

1000 seconds = 0 hours, 16 minutes, and 40 seconds


  1. The file expected.txt contains an example of the desired input and output for our program. Download this file and examine it in a text editor.
  2. Execute your own program in the terminal, providing 1000 as the input value.
  3. Now download the input.txt file and run this command in the terminal: python3 seconds.py < input.txt  and see what happens.
  4. Enter the command  python3 seconds.py < input.txt > output.txt  and see what happens.
  5. You should now have a file named output.txt in that same directory. What is in the file?
  6. Enter the command meld expected.txt output.txt  to compare the two files(meld is used in the Linux labs diffmerge or winmerge can be done on your home machine).
  7. If there are no differences, congratulations on getting your program right the first time!
    (But go back and make a mistake in your Python code to see how this part of the lab works.)
  8. Based on the differences between the expected output and your actual output, figure out what needs to be changed in your code.
  9. Edit and re-execute your code using Thonny. Try testing your improved output using diffmerge.
  10. Repeat the previous step until your solution matches 100%. Then submit your seconds.py file via Gradescope.

Back to Top