Newbie Question about sequence multiplication

S

Scott

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
 
J

John Machin

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

Steve Holden

Scott said:
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
 
B

Bruno Desthuilliers

Scott a écrit :
(snip)
print ' ' * left_margin + '| ' + ' ' sentence + ' |'
^
a '+' is missing here

(snip)
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'

Of course. You can't multiply a string by another string.
 
W

Willy

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 + '+'
print ' ' * left_margin + '|' + ' ' * box_width + '|'
print ' ' * left_margin + '| ' + ' ' + sentence + ' ' + '|'
print ' ' * left_margin + '|' + ' ' * box_width + '|'
print ' ' * left_margin + '+' + '-' * box_width + '+'
print
 
A

attn.steven.kuo

(snipped)
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

(snipped)


Others have pointed out the mistake. I'll just add that in
this particular case, one might want to pad lines with some
whitespace and change the middle print statement to:

print
print ' ' * left_margin + '+' + '-' * box_width + '+'
print ' ' * left_margin + '|' + '-' * box_width + '|'
print ' ' * left_margin + '|' + sentence.center(box_width) + '|'
print ' ' * left_margin + '|' + '-' * box_width + '|'
print ' ' * left_margin + '+' + '-' * box_width + '+'

to improve legibility.
 
7

7stud

copied straight from "Beginning Python: From Novice to
Professional",

I suggest you get another book. I am currently reading that book, and
unless you are an experienced programmer that can detect all the
mistakes, and you have another book like "Python in a Nutshell" to
fill in all the gaps, I don't think you can learn python from that
book.

I recently looked at Learning Python in the bookstore, and it seems a
lot better. Unfortunately, it doesn't reflect the major changes in
python over the last couple of years, but I would still recommend it
over Beginning Python: From Novice to Professional.
 
P

Peter Otten

Scott said:
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
Now that, even though copied straight from "Beginning Python: From Novice
to Professional", returns :
There's an error in your program: invalid syntax

The source code available at

http://hetland.org/writing/beginning-python/

does work.

Peter
 
S

Scott

Thanks to everyone that responded....I would never have figured that out.

7stud,
Your suggestion is being considered lol, as there are a lot more bits of
code in that book that I can't get running correctly.
Any other books you'd, or anyone for that matter, would recommend as
required reading? Free would be very very (read very) good, as you've
already said the one I have isn't very viable and it cost me $50.
Basically, what I'm saying is that if I go and spend another $50+ my wife is
going to make it very hard to learn Python; after all, how much can I learn
with a size 5 down my throat? lol


Now when suggesting books, keep in mind that, that while I'm new to Python
(and programming in general) I'm able to grasp difficult concepts as long
as I have enough detail as to why it is the way it is. For instance I'm, by
experience and nature, a computer technician and communications specialist.
I've studied, everything from childrens walkie talkie to deep space
satalittes back to how computers talk (which is why I'm here now trying to
learn the language of computers). And all that just because I have a
unquenchable desire to know. SO, with that all said, the more details the
better. If you have a book with 4 chapters on functions......I want to read
it.

Any help would be greatly appreciated. As I've said, this is something that
I feel I have to know.
 
S

Steve Holden

Scott wrote:
[...]
Now when suggesting books, keep in mind that, that while I'm new to Python
(and programming in general) I'm able to grasp difficult concepts as long
as I have enough detail as to why it is the way it is. For instance I'm, by
experience and nature, a computer technician and communications specialist.
I've studied, everything from childrens walkie talkie to deep space
satalittes back to how computers talk (which is why I'm here now trying to
learn the language of computers). And all that just because I have a
unquenchable desire to know. SO, with that all said, the more details the
better. If you have a book with 4 chapters on functions......I want to read
it.

Any help would be greatly appreciated. As I've said, this is something that
I feel I have to know.
"Dive into Python" is probably the best free read about Python for
programmers. It takes you in deep much more quickly that the tutorial,
but if you can understand it you develop quite a sophisticated
understanding of the language in a fairly short time.

regards
Steve
 
S

Steve Holden

Scott wrote:
[...]
Now when suggesting books, keep in mind that, that while I'm new to Python
(and programming in general) I'm able to grasp difficult concepts as long
as I have enough detail as to why it is the way it is. For instance I'm, by
experience and nature, a computer technician and communications specialist.
I've studied, everything from childrens walkie talkie to deep space
satalittes back to how computers talk (which is why I'm here now trying to
learn the language of computers). And all that just because I have a
unquenchable desire to know. SO, with that all said, the more details the
better. If you have a book with 4 chapters on functions......I want to read
it.

Any help would be greatly appreciated. As I've said, this is something that
I feel I have to know.
"Dive into Python" is probably the best free read about Python for
programmers. It takes you in deep much more quickly that the tutorial,
but if you can understand it you develop quite a sophisticated
understanding of the language in a fairly short time.

regards
Steve
 
L

lancered

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top