Re: Newbie Question about sequence multiplication



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

.



Relevant Pages

  • Re: DataView Web parts
    ... menu on right click is Format Item As. ... > to do to actually make it a functioning WSS W eb Part. ... > assume there's one last step I'm missing. ... I opened the page in the browser and what was created as the Data View ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: Formatting numbers with leading and trailing zeros
    ... If the missing leading and/or trailing zeroes were consistent in how many were missing, then the suggestion David gave you would be the way to restore them; however, if the number of leading and/or trailing zeroes can vary within a given column, then there is nothing you can do to restore the information that is not there. ... If you want to format all the numbers with 2 decimal places you can do that. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Need modern version of old technique to show missing values
    ... an ascii or formatted file record can be checked in each field ... this "missing value". ... field or the dot in fixed format fields. ... Matlab uses NaN but propogates NaN unless coded to exclude them. ...
    (comp.lang.fortran)
  • Re: What Advantages Does .docx have over .doc? Any?
    ... A Word .doc file saved to .docx should shrink by about 75 per cent. ... If you have tracked changes on and you MOVE a picture, ... The .docx format is compressed Unicode text, ... Am I missing something? ...
    (microsoft.public.mac.office.word)
  • Re: Missing tools in SSMS 2005
    ... >I am using SSMS since a few weeks, and I found it is missing a few ... > The worst of all, anyway, is that we still can't apply a customized format ... > to the sql documents. ... > tool and a tool to recommend. ...
    (microsoft.public.sqlserver.tools)