|(as it would appear on official course roster)|
|Umail address:||@umail.ucsb.edu|| section
| 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)
|true||Thu 01/24 02:00PM||Wed 01/30 09: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,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the lowest scores (if you have zeros, those are the lowest scores.)
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.
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 the
input()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 of
input()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 value
(20 pts) Read about the
elsestatements 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
evalfunction calls; I don’t want to scare you too much about that. The
evalfunction 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
elseworks, consider this code, which is incomplete. Add the code that is needed so that if the user puts in something other than
CAfor 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
rangefunction 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.)
In the space below, write a few lines of Python that uses a loop with the
rangefunction to print the values shown in the box at right. (Your answer must use a loop with
rangeto get credit.)