@extends('template') @section('title') L2P2: Text in boxes @stop @section('content') # Lab 2: Part 2: Text in boxes ## Note on Labelling The sub-tasks in this section are marked Partner A or Partner B to indicate how to work on them as a pair and when you should switch "drivers." One of the members of your team should be writing code for the parts marked Partner A and then the other member should write code for the parts marked Partner B, to ensure you both get hands-on practice with today's concepts. If you happen to be in a team of 3, just switch who is writing code for each different segment, but make sure all members of the team get a chance to write code. You may have to dictate some code to each other when one part depends on a previous part, but we've arranged the parts to minimize this. Of course, whoever is not writing code should be helping out by suggesting things to try and helping to proofread the code that's being written. ## Overview You'll first start with built-in python functions [`input`](/reference/builtins#input) and [`len`](/reference/builtins#len), and then combine those functions to draw boxes around a user-supplied sentence. ## Task A: Get comfy with `input` The python built-in function [`input`](/reference/builtins#input) allows you to ask the user for input. Here is a [list of all the built-in Python functions](https://docs.python.org/3/library/functions.html) and here is [our quick-reference page for the built-in functions you'll need in this course](/reference/builtins). Try this: ```py input('Type your name') ``` produces this: ```py Type your name # notice that python is waiting for you to respond ``` Note: you can add an arrow to the prompt, if you like, and it's usually useful to add a space at the end of the prompt, so that the user doesn't naturally type a space themselves, which would count as part of their input: ```py input('Type your name ==> ') ``` produces ```py Type your name ==> # waiting for you to type something and hit return ``` Let's say the user types `Selena`. In order to refer to `Selena` later in your program, you'll need to save it in a variable. ### Task A1
Your name? ==> Selena
Hi, Selena, welcome to cs111!
+ Example 2: the user types `Calvin and Hobbes`
Your name? ==> Calvin and Hobbes
Hi, Calvin and Hobbes, welcome to cs111!
### Task A2
Your thoughts? ==> Selena
6
+ Example 2: the user types `Sometimes you feel like a nut`
Your thoughts? ==> Sometimes you feel like a nut
29
Now that you are familiar with both [`input`](/reference/builtins#input)
and [`len`](/reference/builtins#len), we can do something a bit more
complex.
## Task B: Draw a box around the user input
Enter a string ==> Hi
**************
* Hi *
**************
Sample 2:
Enter a string ==> Lovely day today
****************************
* Lovely day today *
****************************
Sample 3:
Enter a string ==> Sometimes you feel like a nut
*****************************************
* Sometimes you feel like a nut *
*****************************************
Let's take another a close look at Sample 3 above.
There should be 5 spaces of padding on either side of the string in the
box (see enlarged image below). Note that your program should work
correctly regardless of the length of the provided string.
## Task C: Draw a box around TWO user inputs
Enter your first string ==> happy
Enter your second string ==> birthday to you!
****************************
* happy *
* birthday to you! *
****************************
Example 2:
Enter your first string ==> Row row row your boat
Enter your second string ==> gently
*********************************
* Row row row your boat *
* gently *
*********************************
Example 3:
Enter your first string ==> Sometimes you feel like a nut
Enter your second string ==> Sometimes you do not
*****************************************
* Sometimes you feel like a nut *
* Sometimes you do not *
*****************************************
@include('/labs/lab02/_toc')
@stop