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.
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
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:
Algorithm
Main Steps: (each of these will be a function will be called from the main program.)
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.
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:
To print the poster (frame):
Use the methods to print the poster:
To print the body of the poster (frame):
__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()