CSci 157 Homework #1

Due: January 31, 2025

This assignment is worth 50 points, and should be turned in by the beginning of class for full credit. All homework is to be pledged.

Reading. Zelle, Section 1.7 and Chapter 2, Sections 2.1 through 2.5 (also posted on BrightSpace)

Problems. Some exercises are simple experiments with one line answers - use complete sentences where appropriate.
  1. (6 points) In a recent lecture, we looked at examples of three data types: the numeric types int and float, and the text type str. Data types are defined by the set of values they include and the set of operations that are allowed on those values. For example, the usual arithmetic operations like + and - are valid on both number types. It turns out that programming languages often redefine these common operators for non-numeric data types as well.

    Try these uses of the arithmetic operators on strings in the Python shell, and show the results. If the result is an error, show the last line of the error message.

    "hot" + "dog"
    "in" - "out"
    "hot" * "dog"
    "hot" * 5
    "repeat" / 3
    "too" ** "big"

     
  2. (6 points) Say which of the variable names given below are valid, and which are invalid, in Python. Careful - at least one is a Python keyword (these are listed in Zelle Section 2.3)
    1. pay$me
    2. one_thing
    3. 1thing
    4. None
    5. _con_man
    6. This_is_not_valid_Python
    7.  
  3. (6 points) Using gedit or your favorite text editor, enter the program shown below (note: indentation is required in Python, so make sure everything lines up as shown).
    # File: chaos.py
    # A simple program illustrating chaotic behavior.

    def main():
        print("This program illustrates a chaotic function")
        x = float(input("Enter a number between 0 and 1: "))
        for i in range(10):
            x = 3.9 * x * (1 - x)
            print(x)

    main()
    Save the program in a file called chaos.py, then use it to answer these questions:
    1. Run the program using this command: python chaos.py
      When prompted by the program, enter the value 0.25 and compare to the results given in the text (page 14). Do you get the exact same sequence of values?
    2. Run the program again, and when prompted this time, enter the value 0.81. List the numbers it computes.
    3. Run one last time, and when prompted enter your name instead of a number. What happens? If the result is an error, show the last line of the error message.
       
  4. (4 points) The program in the previous question uses the Python function input to prompt the user for input from the keyboard.  Typically the input is saved in a variable using the assignment statement.

    Record your guess as to the type of the value being returned by input and saved in the variable age.
    age = input("How old are you? ")
    Now execute the statement in the Python shell, and determine the actual type of the value stored by age; record the answer. Were you correct?
     
  5. (4 points) If the input 0.25 to the chaos.py program had actually been a string, the program would have failed. This is the reason the program uses the function float along with input to get the value used in its computation. Briefly describe, in your own words, what you think the function input actually does.

  6. (8 points) Line 7 of the program starts with the keyword for. Notice that lines 8 and 9 are indented a few spaces farther than the for line. Indentation is important to the structure of statements in Python. To see how, do the following:   
    1. Remove the indentation so lines 8 and 9 start at the same column as for. Try running the program; record the resulting error. 
    2. Re-indent line 8 but leave line 9 where it is. Run the program again and record the result. Why do you think this happened?
    3. Based on your reading of Section 1.7 and the output of the program, what do you think the for statement is doing with respect to lines 8 and 9 ?
       
  7. (6 points) At the beginning of Chapter 2, Zelle lays out the steps in developing a software program. Write out the name of each step, numbering them 1 through 6.

  8. (3+3+4 points) 
    1. Give the number of those steps that involve actually writing or modifying the program in our programming language.
    2. Give the number of those steps that require thinking about the problem rather than the solution (the program itself).
    3. Some of these steps are iterative, which means that once complete, we often find we need to go back to the beginning of the step and start the process over. Which steps do you expect to be iterative?

Turning in Your Work. You can turn in written exercises one of these ways: