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.
- (6 points) In a recent lecture, we looked at examples of
three data types: the numeric types
int
andfloat
, and the text typestr
. 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"
- (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)
- pay$me
- one_thing
- 1thing
- None
- _con_man
- This_is_not_valid_Python
- (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
Save the program in a file called chaos.py, then use it to answer these questions:
# 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()
- 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? - Run the program again, and when prompted this time, enter the value 0.81. List the numbers it computes.
- 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.
- Run the program using this command: python
chaos.py
- (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 byinput
and saved in the variableage.
age = input("How old are you? ")
Now execute the statement in the Python shell, and determine the actual type of the value stored byage
; record the answer. Were you correct?
- (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 withinput
to get the value used in its computation. Briefly describe, in your own words, what you think the functioninput
actually does.
- (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 thefor
line. Indentation is important to the structure of statements in Python. To see how, do the following:
- Remove the indentation so lines 8 and 9 start at the same column
as
for
. Try running the program; record the resulting error. - 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?
- 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 ?
- Remove the indentation so lines 8 and 9 start at the same column
as
- (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.
- (3+3+4 points)
- Give the number of those steps that involve actually writing or modifying the program in our programming language.
- Give the number of those steps that require thinking about the problem rather than the solution (the program itself).
- 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:
- complete the assignment on paper and turn in at the beginning of class on the due date
- complete the assignment in a text editor and post to the Assignments page on BrightSpace by the due date
- in both cases, make sure your name is clearly visible