1 |
h04 |
CS8 F17 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | |||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h04: Perkovic 3.1-3.2 (print, input, if/else, for)
ready? | assigned | due | points |
---|---|---|---|
true | Tue 10/17 08:00AM | Tue 10/24 08:00AM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE.
There is NO MAKEUP for missed assignments, and you may not submit work in advance, or on behalf of another person.
In place of that, we drop the four lowest scores (if you have zeros, those are the four lowest scores.)
READING ASSIGNMENT
Please read Perkovic 3.1-3.2 (print, input, if/else, for). Then complete these problems and turn in your completed homework during your registered lab section..
(10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class. It is important to fill in both name and umail every time, since handwriting is sometimes difficult to decipher. Having both helps us ensure you get credit for your work.
Also: while we strongly prefer that you submit your homework on a single sheet of paper, if you MUST submit it on multiple sheets, JUST write your name at the top of both sheets and turn in both sheets UNCONNECTED.
DO NOT staple, paper clip, spit-fold-and-tear, or do ANYTHING that would make it difficult to automatically feed your paper through a scanner.
-
(10 pts) Section 3.1 discusses a feature of IDLE called “restarting the shell”. What does the book say happens when you restart the shell?
-
(10 pts) Section 3.1 also discusses the
input()
function, which is used to get input from the user. It indicates that the result of theinput()
function always comes back as a particular data type, no matter what the user types in. What is this data type? -
Section 3.2 discusses the fact that you can nest control structures in Python.
-
(10 pts) What does it mean to nest control structures in Python?
(Note: you won’t find the answer spelled out in the text. It is implied, but not clearly explained. So you’ll have to do some thinking to figure out how to answer. That’s on purpose.)
-
(10 pts) What can you see in the Python code (or do you have to write in your own Python code) to indicate that something is “nested”? (This is also in the text, but implied rather than stated.)
-
-
(10 pts) Section 3.2 discusses iteration, which means doing things over and over again, in a loop. As we will learn, there is more than one kind of loop in Python, but there is a particular kind of loop discussed in Section 3.2 that allows us to process each character in a string, or each item in a list, one at a time.
What is the Python word that starts that kind of loop?
-
(10 pts) On p. 56-57, the textbook suggests using the
eval()
function to convert the result ofinput()
from the user if it going to be used as something other than a string (e.g. as a number, list, etc.)THIS IS A POTENTIALLY DANGEROUS HABIT TO GET INTO. DON’T DO IT.
(We’ll discuss this in more detail in lecture, but the short version is: this is the kind of habit that leaves security vulnerabilities open for hackers. There isn’t any real danger in an intro course, because the software you write won’t be run by anyone except you and your TA, but in general, this is VERY bad habit to get into.)
So what can we do instead? Well, you actually already saw one option—a technique that was discussed in Section 2.4. Suppose we have this Python statement:
ageAsString = input('Please enter your age in years: ')
Using techniques from Section 2.4, but NOT using
eval
, what line of Python code can you write to convert the valueageAsString
into anint
value? -
(20 pts) Read about the
if
andelse
statements in Section 3.2, including what happens when the various programs in Section 3.2 are run.(If you want to run them yourself to see what happens, I strongly encourage that! And: it is ok to type them in and run them, even including the
eval
function calls; I don’t want to scare you too much about that. Theeval
function is only risky when someone you don’t trust is putting input into your code, e.g. when you are making a app that the general public will use.)Once you think you understand how
if
andelse
works, consider this code, which is incomplete. Add the code that is needed so that if the user puts in something other thanCA
for the state, the code will print “Out of State Tuition”. Be sure to indent your code properly, and include all needed punctuation.homeState = input("Please enter your state: ") if homeState=="CA": print("In State Tuition")
-
(10 pts) Section 3.2 discusses how to use the
range
function with a loop so that you can cause a variable to take on a sequence of values—the sample code illustrates this by printing those values. (Later we’ll do other things with those sequences of values such as adding them, or multiplying them, or using them to index into a list or string, or to draw things.)3 5 7 9
In the space below, write a few lines of Python that uses a loop with the
range
function to print the values shown in the box at right. (Your answer must use a loop withrange
to get credit.)