Newbie Question about sequence multiplication

lancered wangday at gmail.com
Sun Apr 8 06:36:03 EDT 2007


On Apr 5, 12:19 am, "Scott" <s_brosci... at comcast.net> wrote:
> Alright, so I've been trying to teach myself Python which, when compared to
> my attempt to learn C++, is going pretty well.
> But I've come across an issue that I can't figure out, so I figured I'd ask
> the pro's.
>
> Now it looks pretty weird in this format but it was copied exactly from IDLE
>
> *****code follows*******
>
> #What this program is suppose to do is print a sentence centered in a box
>
> sentence = raw_input('Sentence: ')
>
> screen_width = 80
> text_width = len(sentence)
> box_width = text_width + 6
> left_margin = (screen_width - box_width) // 2
>
> print
> print ' ' * left_margin + '+'   + '-' * (box_width-2)  +  '+'
> print ' ' * left_margin + '|  ' + ' ' * text_width     + ' |'
> print ' ' * left_margin + '|  ' + ' '   sentence       + ' |'
> print ' ' * left_margin + '|  ' + ' ' * text_width     + ' |'
> print ' ' * left_margin + '+'   + '-' * (box_width-2)  + ' |'
> print
>
> ****end code****
>
> Now that, even though copied straight from "Beginning Python: From Novice to
> Professional", returns :
> There's an error in your program: invalid syntax
>
> with the word sentence highlighted (not the sentence when I'm defining the
> name, the one in......uhm....the body of the code)
>
> Now if i put * before sentence as it is with the rest of the variables, it
> actually gets to the point where it asks me for the sentence, but after
> inputting my sentence I receive:
> Traceback (most recent call last):
>   File "D:/Programming/Python/sequence string multiplication example", line
> 16, in <module>
>     print ' ' * left_margin + '|  ' + ' ' * sentence       + ' |'
> TypeError: can't multiply sequence by non-int of type 'str'
>
> Why can't I get it to do what it's supposed to do? What am I
> missing/misunderstanding?
> Very simply all its supposed to do is something like this (now bear with me
> formating might distort this a bit lol)
> +------------------------------------+
> |                                                        |
> |                      Like This                    |
> |                                                        |
> +------------------------------------+
>
> Any help would be greatly appreciated
>
> -Scott

I modified the codes a little bit to get it running, and give the
correct alignment.
Just as a reference for you. Here is it:

*********************************************
sentence = raw_input('Sentence: ')

screen_width = 80
text_width = len(sentence)
box_width = text_width + 6
left_margin = (screen_width - box_width) /2

print
print ' ' * left_margin + '+' + '-' * (box_width-2)  + '+'
print ' ' * left_margin + '|' + ' ' * (box_width-2)  + '|'
print ' ' * left_margin + '|' + ' '*2+sentence +' '*2+ '|'
print ' ' * left_margin + '|' + ' ' * (box_width-2)  + '|'
print ' ' * left_margin + '+' + '-' * (box_width-2)  + '+'
print
****************************************************




More information about the Python-list mailing list