Re: Newbie Question about sequence multiplication
- From: Steve Holden <steve@xxxxxxxxxxxxx>
- Date: Wed, 04 Apr 2007 18:48:12 -0400
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 ' ' * 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 + ' |'How on Earth are you running these programs? That doesn't look like a compiler error message!
print ' ' * left_margin + '+' + '-' * (box_width-2) + ' |'
****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 theThe plus sign I've indicated adds (concatenates) the sentence you've typed in to the other strings you are using.
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 IGood luck!
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
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
.
- References:
- Newbie Question about sequence multiplication
- From: Scott
- Newbie Question about sequence multiplication
- Prev by Date: Re: Newbie Question about sequence multiplication
- Next by Date: Re: Why NOT only one class per file?
- Previous by thread: Re: Newbie Question about sequence multiplication
- Next by thread: Re: Newbie Question about sequence multiplication
- Index(es):
Relevant Pages
|