tags:

views:

85

answers:

3

Hello,

I need a string consisting of a repetition of a particular character.At the python console, if i type :

n = '0'*8

Then n gets assigned a string consisting of 8 zeroes,which is what i expect.

But, if i have the same in a python program (.py file), then the program aborts with an error saying 'can't multiply sequence by non-int of type 'str''

Any way to fix this ?

Thank You

A: 

That line of code works fine from a .py executed here, using python 2.6.5, you must be executing the script with a different python version.

João Pinto
This is nothing to do with the python version - it is a bug in his progam, as delnan says.
Dave Kirby
Actually there was an error on the question information on the first place, according to the question he was using '0'*8 which is valid, assuming that information was correct a probably answer was that he was using an 'invalid' python interpreter.
João Pinto
+9  A: 

You get that error because - in your program - the 8 is actually a string, too.

>>> '0'*8
'00000000'
>>> '0'*'8' # note the ' around 8
(I spare you the traceback)
TypeError: can't multiply sequence by non-int of type 'str'
delnan
A: 

I could bet you're using raw_input() to read the value which multiplies the string. You should use input() instead to read the value as an integer, not a string.

kirbuchi