1 |
h04 |
CCS CS20 S18 |
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 (Python Programs), 3.2 (Execution Control Structures)
ready? | assigned | due | points |
---|---|---|---|
true | Tue 04/17 12:30PM | Sun 04/22 12:00PM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
This assignment should be submitted by scanning the pages in the correct order to a PDF file and uploading to gradescope.com.
For more information, visit ucsb-cs8.github.io and look for Gradescope: Student Self Submission under "topics".
Even though it is a Gradescope submission, nevertheless, *please fill in the information at the top of this homework sheet*, including your name and umail address.
READING ASSIGNMENT
Please read Perkovic 3.1 (Python Programs), 3.2 (Execution Control Structures). Then complete these problems and turn in your completed homework in lecture on the due date.
-
(10 pts) These 10 points are for filling in your name at the top of the sheet, and scanning correctly on Gradescope. (Having these here gives me a place to give you feedback if there is some glitch with the way you are doing it.)
-
(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 6 9 12
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.)