CS8, 10F, H05, due Tue Lecture 10.19—Miller/Ranum 3.2 (only pp. 83-93).—Total points: ?

Available online as http://www.cs.ucsb.edu/~pconrad/cs8/10F/homework/H05—printable PDF

Name: (4 pts)   Umail Address: (4 pts)   @umail.ucsb.edu
Lab Section (2 pts)—circle one:  9am   10am   11am   noon   unknown   crashing 
You may collaborate with at most ONE other person on this homework assignment. If you do, please enter his/her name here:  
  (He/she should also enter your name on her/his assignment.)

(Note: For now, circle the lab section you are registered for on GOLD. If you need to request attendance at a different lab section because of an ACTUAL SCHEDULE CONFLICT, please email pconrad@cs.ucsb.edu with details)

This assignment is due in Lecture on Tuesday, 10.19.
It may ONLY be submitted in Lecture, in NH1006 at 2pm on Tuesday.
You must come IN PERSON to turn it in during your assigned Lecture section.

Late Policy: No email submission allowed—and don't "slip it under my door". If you need to make it up, you must do so during office hours, or make an appointment to see me, and you must request this appointment within 48 hours of when the assignment was originally due.

Personal Day/Sick Day policy: Everyone is permitted one "personal day/sick day" when you get to make up a missed homework assignment for free during office hours or via appointment. After that, you may not make up the homework assignment—you can only earn back the points through extra credit opportunities.

(For more details, see the syllabus and the homework policy)

Check this link for chapter 3 typos: j.mp/ch3typos-ppic

Reading: By now, you should have read all of Chapter 2. We are going to start reading a little bit in Chapter 3 even though we aren't completely finished with Chapter 2 yet—specifically, Section 3.2. Strings are very useful features of the Python programming language. They will allow us to talk about many more interesting kinds of problems than just working with Turtle graphics and numbers.

  1. Read pages 83 and 84, then answer these questions

    1. (3 pts) Write an assignment statement that sets the variable name equal to your full name (first and last name).

    2. (3 pts) Write an assignment statement that sets the variable movieTitle equal to the value Ocean's Eleven
      The apostrophe (single quotation mark) inside the word Ocean's should be part of the string.

    3. (3 pts) Write an assignment statement that sets the variable message equal to the following value:
      The quotation marks around the title of the movie should be part of the string.

      The movie tonight is "Toy Story 3"

  2. (3 pts) Read Section 3.2.1 on p. 85, then answer this question.

    Set the variable message equal to the value Tonight's movie is "Ocean's Eleven"
    Both the ' and " symbols should be part of the string's contents.
    Hint: consider using concatenation and building the result up in pieces that you combine together.
    Or, research how to use the backslash (\) to "escape" quotations marks that appears in the middle of strings.

    Also: type both the name message and print(message) at the Python prompt to check the value—notice how they look different. You may want to look up what the backslash (\) means in the context of Python strings.

  3. (1 pts) Section 3.2.3 talks about the index operator. What symbol is used for the index operator?

  4. (2 pts) Section 3.2.4 talks about the slice operator. What symbol is used for the slice operator?

Please turn over for more...

...continued from other side

  1. +---+---+---+---+
    |   |   |   |   |
    |   |   |   |   |
    |   |   |   |   |
    |   |   |   |   |

    4 x 4 grid

    (3 pts) Suppose we wanted to print a checkerboard pattern on the screen (using normal screen characters, not graphics). We might do it like this if we wanted a four by four grid (see the box at right).

    The line between each row is made up of the pattern +---+---+---+---+
    which consists of +--- four times followed by +

    In Python, you could store this pattern in a variable called rowBoundary by using the following assignment statement:

    rowBoundary = "+---+---+---+---+"

    But what if we wanted the top row of a 5x5 or an 8x8 grid?
    Typing all those +--- characters could get tedious—and we might lose count.

    So, read Section 3.2.2 (pages 85-86), and another way may occur to you—a way that would be handy, if for example, you needed to set the variable pattern to +--- 6, 8 or 100 times followed by a +, without much typing. This other technique involves using the * operator and the + operator.

    Illustrate this technique by writing a Python assignment statement
    setting rowBoundary equal to 10 occurrences of +--- followed by a +.

    Be sure to distinguish between the + symbol used as an operator (meaning concatenation) and the + symbol used as something we want to print to make a pattern (i.e. part of the string we are assigning to the variable pattern.)

  1. If we write this at the Python shell prompt:

    >>> team = "Gauchos"

    then type each of the following at the shell prompt, what will the result be?

    (Note: try to answer without doing this online—then check your answers online. You'll need that skill for the next midterm exam!)

    1. (2 pts) >>> team[0]

    2. (2 pts) >>> team[0:2]

    3. (2 pts) >>> team[-1]

    4. (2 pts) >>> team[1:3]

  2. (3 pts) Read Section 3.2.5. Then answer:

    There is a string method that will convert the variable team to all capital letters,
    i.e. return 'GAUCHOS' instead of 'Gauchos'?

    What do we write if we want to assign the variable teamUC to be team, converted to upper case?

    Hint: The correct answer is NOT
    teamUC = "GAUCHOS"

  1. Read Section 3.2.6 about Character Functions, and then answer these questions:

    1. (2 pts) What is the value of ord('b')

    2. (2 pts) What is that the value of ord('+')?

    3. (2 pts) What is the value of chr(70)

    4. (2 pts) What is the value of str(70)

    5. (3 pts) What is the difference between chr(x) and str(x) where x is a variable containing some number?

      (Describe this in your own words—briefly. I want just enough to show that you understand what you read in Section 3.2.6 about chr and str).


End of H05