Newbie Question about sequence multiplication

John Machin sjmachin at lexicon.net
Wed Apr 4 18:48:05 EDT 2007


On Apr 5, 8: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       + ' |'

You didn't say which occurrence of 'sentence' was causing the drama,
but it appears to be the above statement.

The syntax is sequence * factor, where 'factor' is an integer. So both
these are wrong:
'' sentence # needs an operator between ' ' and sentence
' ' * sentence # sentence is not bound to an integer

You need something like this:

print ' ' * left_margin + '|  ' + sentence + ' |'
# plus or minus a space or two


> 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",

Are you sure? Check the accuracy of your "copy"; check the book's
known errata (if any) on the book's website (if any).

> 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?

Seeing you asked:
1. Not following the recipe exactly and/or not verifying accuracy of
recipe.
2. Not understanding what the * operator is doing in this context
(sequence * int); does the book not explain what this does?

> 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





More information about the Python-list mailing list