[Tutor] trouble with "if"

Eric Walstad eric at ericwalstad.com
Wed May 23 19:42:53 CEST 2007


Hi Adam,

adam urbas wrote:
> when I input a radius, it says:
> 
> can't multiply sequence by non-int of type 'float'
...
> > radius=raw_input("Enter Radius:")
> > diameter=(radius*2)


After you collect the raw_input for the radius, the radius variable
contains a string, not a number (that's what '<type 'str'>' means).
Python is calling the string a sequence in your error message.

Try converting your radius to a float type first:

radius=float(raw_input("Enter Radius:"))


As side notes: those '>>>' characters in previous responses are what the
python interactive terminal displays as its command prompt.  The
'type()' function tells you the data type of a variable.

Here's an example of using the Python interactive terminal to debug your
issue (give it a try yourself, but don't enter the '>>>' in the terminal):

ewalstad at sawarna:~$ python
Python 2.5.1 (r251:54863, May  3 2007, 12:27:48)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> radius=raw_input("Enter Radius:")
Enter Radius:5
>>> radius
'5'
>>> type(radius)
<type 'str'>
>>> diameter=(radius*2)
>>> diameter
'55'   # That is, it is giving you the string '5', two times
>>> type(diameter)
<type 'str'>
>>>
>>>
>>> radius=float(raw_input("Enter Radius:"))
Enter Radius:5
>>> radius  # radius now contains a float type
5.0
>>> type(radius)
<type 'float'>
>>> diameter=(radius*2)
>>> diameter
10.0
>>> type(diameter)
<type 'float'>
>>>



More information about the Tutor mailing list