06.0 Python‎ > ‎Python Projects‎ > ‎

Frame This

Hint: You may be able to use the last exercise of section 10 def: as a starting point.

In the "Frame This" project you will be investigating Strings and how to manipulate them. Python has 5 other types of built-in data types the can be managed the same way as Strings. The skill set you will practice here on Strings will allow you to also manipulate: byte sequences, byte arrays, lists, tuples, range objects.

Objectives

The student will learn and use various String function to manipulate strings.
In the future, the student will use these skills with some of the other Python Sequence Data Types:  byte sequences, byte arrays, lists, tuples, range objects

Project Overview

Write a program that will write a phrase centred left to right in a rectangle of *. 
The program will accept two lines of input: 
    the width of the box 
    the phrase

The program will:
  • accept keyboard input from the user for the width of the box
  • accept keyboard input from the user for the phrase
  • split the phrase up so that it will fit into the box 
  • centre each line of the phrase in the box
  • ensure there is one space between each line of the phrase and of edge box
Algorithm
Main Steps: (each of these will be a function will be called from the main program.)
  1. Prompt the user for the width of the box
  2. Prompt the user for the phrase.
  3. Determine longest word in the phrase
  4. if the longest word does not fit, issue an message to the and give the user the option to continue with the minimum allowable size  
  5. if possible, display the poster
You will need to determine the algorithm for each of these methods.

Sample:
    input:
        13
        Hello, how are you?

    output:

*************
*   Hello,  *
*  how are  *
*    you?   *
*************

   input:
        3
        Hello, how are you?

    output:
        The Frame must be at least 10 characters wide,
        would like to change the width to 10? y/n

if the users enters Y the output will be;

**********
* Hello, *
*  how   *
*  are   *
*  you?  * 
**********
otherwise there is no output.

Project Road Map

Hint: The Tool Box for Sequential Data Types investigation may help you IF you are having problems.

Use the template below to start your program.

The main program includes the following steps:
  1. Enter a width and a phrase. (To start, make sure the phrase will fit into the poster.)
  2. Ensure the width is wide enough
  3. Print the poster
To print the poster (frame):
Use the methods to print the poster:
    1. print the top of the poster
    2. print body of the poster
    3. print the bottom of the frame. (this is the same as the top of the box)
To print the body of the poster (frame):
  • set the line to empty
  • for each word in the list
  • if the word is added to the line and it still fits
  • add the word to the line
  • else
  • print the line
  • set the line to empty
  • print the last part of the phrase

Hints: 

__author__ = 'Mister V'

def lengthOfLongestWord(frase):
# DO NOT change this method without permission
# this method uses string method .partition
longestLength= 0
while len(frase) > 0 :
first_word = frase.partition(" ")[0]
frase = frase[len(first_word)+1:len(frase)]
lengthOfWord = len(first_word)
if lengthOfWord> longestLength:
longestLength = lengthOfWord
return longestLength

def topOfFrame(w):
# DO NOT change this method without permission
print ("*"*w)

def bottomOfFrame(w):
# DO NOT change this method without permission
# notice that the top and bottom of the frame are the same,
# so have topOfFrame do the work again
topOfFrame(w)

def bodyOfFrame(w,frase):
#this is here you will break the phrase up into shorter lines that will fit in the frame
print (frase)

def framePhrase( w, frase):
# DO NOT change this method without permission
topOfFrame(w)
bodyOfFrame(w,frase)
bottomOfFrame(w)

def main():
# DO NOT change this method without permission
width = int(input('Enter the width of the poster: '))
phrase = input('Endter the Phrase: ')
minimumFrameSize = lengthOfLongestWord(phrase) + 4

if width < minimumFrameSize:
width = minimumFrameSize
framePhrase(width, phrase)

if __name__ == '__main__':
main()
Comments