@extends('template') @section('title') Text in boxes @stop @section('content') # Lab 1: Text in boxes ## 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: ```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 Write a couple of lines of Python that asks the user for their name, and then prints a friendly greeting. Here are some examples. + Example 1: the user types `Selena` ```py Your name? ==>Selena Hi, Selena, welcome to cs111! ``` + Example 2: the user types `Calvin and Hobbes` ```py Your name? ==>Calvin and Hobbes Hi, Calvin and Hobbes, welcome to cs111! ``` ### Task A2 Another useful built-in python function is [`len`](/reference/builtins#len). `len` tells you how long a given string is. For example, + ```py len('hello') ``` evaluates to 5 and + ```py len('chocolate cake') ``` evaluates to 14 (spaces and punctuation count). Write a couple of lines of python that asks the user for their thoughts, and then prints the length of the user's thoughts. Below are some examples of how your program should work: + Example 1: the user types `Selena` ```py Your thoughts? ==>Selena 6 ``` + Example 2: the user types `Sometimes you feel like a nut` ```py 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 In this task, you will write a short Python program that asks the user to provide a string, and then prints a box of asterisks (*) around the string. Below are three different examples of what your program should do: Sample 1: ```py Enter a string ==> Hi ************** * Hi * ************** ``` Sample 2: ```py Enter a string ==> Lovely day today **************************** * Lovely day today * **************************** ``` Sample 3: ```py 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. An image illustrating the number of spaces and counting each space to show that there are exactly 5 on each side of the middle text. If you can configure your screen reader or other display to show per-character information, this information should be availble from the text above as well. ## Task C [OPTIONAL]: Draw a box around TWO user inputs In this task, the user is asked TWICE to provide strings. Your program will produce a box around both lines of text, with 5 spaces of padding around the longer string in the box. See examples below. Hint: Python's built-in [`max`](/reference/builtins#max) function can be helpful here. ```py >>> max(10, 2) 10 >>> max(2, 25) 25 ``` Example 1: ```py Enter your first string ==> happy Enter your second string ==> birthday to you! **************************** * happy * * birthday to you! * **************************** ``` Example 2: ```py Enter your first string ==> Row row row your boat Enter your second string ==> gently ********************************* * Row row row your boat * * gently * ********************************* ``` Example 3: ```py 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/lab01/_toc') @stop