CSci 157 Homework #6

Due: March 27, 2025. This assignment is worth 50 points.

Reading:Zelle, Chapter 8, Sections 8.1-8.5
For some problems you'll find this useful: Sections 5.1 - 5.7 of ThinkCS - Swarthmore Edition

Written Problems (38 points). For the problems in this section, work through any code given by hand; try not to use the computer at all.

  1. (6 points) The terms incremental development, scaffolding, and composition are introduced in the ThinkCS - Swarthmore Edition reading. Explain what is meant by these terms.
     
  2. (6 points) A function that computes the absolute value of a number x is introduced in the reading:
    def absolute_value(x):
        if x < 0:
            return -x
        elif x > 0:
            return x
    By the way, though many examples you'll see do this, I am not a fan of return statements in different clauses of an if statement, because it is all too easy to forget the return if you add another "branch" to the if structure, like another elif clause. Sometimes it's necessary, but often it can be avoided. My preferred way to write this works the same, but has only one return statement at the end:
    def absolute_value(x):
        if x < 0:
            result = -x
        elif x > 0:
            result = x
    return result
     
    Pick either version of the function and copy into the Python shell. Test it by calling it with a positive number, a negative number, and zero; give the results. For example:

    >>> absolute_value(10)
    10

     
  3. (4 points) One of the tests in Problem 2 should have failed to produce the correct result. Figure out how to fix this, and write out the corrected function in your written solutions. There are many ways to do this; the best requires changing two characters, and the simplest requires adding just one character.

  4. (4 points) Without running the Python code below, determine how many times the loop body will be executed. GIve your answer as a complete sentence.
        x = 5
    i = 1


    while i > 20:
    print( i*x )

    i = i-1
  5. (4 points) Repeat Problem 4 for this slightly modified version of the loop (hint: you really don't want to try running this code).
        x = 5
    i = 1


    while i < 20:
    print( i*x )

    i = i-1
  6. (4 points) Modify the code from either of the previous questions so that it properly prints the first 20 multiples of 5. You shouldn't need to add any new lines, just fix the lines that are there. Write out the corrected function.
     
  7. (4+1 points) Show the output produced by this code if the user enters 175 each time the program prompts for input. Give the number of times the loop body executes in this case.
       '''record a set of debit card transactions'''
    bankBalance = 500.00

    while bankBalance > 0: debitAmt = float(input("Debit Amt: "))
    print(" - $", debitAmt)
    bankBalance = bankBalance - debitAmt

    if bankBalance < 0:
    print("You're overdrawn by $", -bankBalance)
  8. (5 points) The code below accumulates a sum of numbers entered by the user, for example tallying the total cost from items scanned at check-out in a store. Show the output produced if the user enters the values 2.5, 3.5, 15.99, 12, 0 in that order. Explain how the value more is computed.
      i = 0
    sum = 0.0
    x = float(input("Enter a value, or zero if nothing to enter: "))
    more = (x != 0)

      while more:
    sum = sum + x
    i = i + 1
    x = float(input("Enter another value, or zero if no more to enter: "))
    more = (x != 0)

    print(str(i) + " items with total cost $", sum)

Programming Problems (12 points).

Reminder: For all programs, follow the Python coding standards and function design methodology given in the Function Definitions notes. Be sure to start any Python files with a comment or docstring containing the program filename, a brief description of the program, and your name.

  1. In the first few labs we wrote scripts to compute windchill values and the area of a circle. Write two functions to compute these values. Instead of an input statement, the information needed will be passed to the function using one or more parameters, and the functions will return their results rather than print them. The correct place to do input and output for most programs like this is in main.

    Now, add a main function to do just that. Test each function with at least three different input values. Consider the following:
    • How does the area calculation behave if the given radius is 0 or a negative number? How should it behave? Give it a think, then add code to the function to check for an appropriate value before computing the area. If you get a negative radius, just return 0.
    • How do you know if your windchill tests work? Turns out, the National Weather service publishes a table of windchills for various temperatures and wind speeds. Test with three combinations of these values.
       
    We can see from the windchill chart that windchills are not defined for temperatures over 40 or wind speeds more than 60. Obviously, negative temperatures are okay, but negative wind speeds are not. Add checks to your windchill function to see if the parameters are in the appropriate range for calculating a meaningful windspeed; if any check fails, return the value None. Also, if the given wind speed is 0, just return the temperature without doing the calculation. Test that the function does the right thing when one or both parameters are out of range.

Turning in Your Work

Written Problems. Turn in written exercises one of these ways:

Programming Programs. Post your Python files (the .py files, not screenshots) to the Assignments page on BrightSpace either individually or combined into a single Zip file. Be sure your name is in the comment at the top of each file.