Newbie Question about sequence multiplication

Steve Holden steve at holdenweb.com
Wed Apr 4 18:48:12 EDT 2007


Scott 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       + ' |'

                                         ^
There's a plus sign missing just here ..|

> 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
> 
How on Earth are you running these programs? That doesn't look like a 
compiler error message!

> 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'
> 
The plus sign I've indicated adds (concatenates) the sentence you've 
typed in to the other strings you are using.

> 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
> 
Good luck!

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list