HeatwaVe
HeatwaVe

Reputation: 3

Why is this piece of simple code not working

I have errors with this program. Can you help me explain what is going on because the system seems to confuse my variables as strings. I have tried changing the variables but it always seems to stop working.

# Area of rectangle
a = input("What is the length of side a in centimeters")
b = input("What is the length of side b in centimeters")
area = a * b
print(area)

It gives me this response

line 5, in <module>
    area = a * b
TypeError: can't multiply sequence by non-int of type 'str'

Given my amateur coding status, all I take from this is that its trying to multiply strings where there are no strings.

Upvotes: 0

Views: 79

Answers (4)

Jon Kiparsky
Jon Kiparsky

Reputation: 7773

The previous answers are correct in that the simple fix is to cast the input to an int value. However, this error is a little mysterious:

TypeError: can't multiply sequence by non-int of type 'str'

and deserves to be explained.

What's happening here is that python understands a string to be a sequence of characters. This is true even of a single character, eg 'a' or of no characters, eg '' - you don't generally make use of the underlying character type in python.

And it turns out that in python you can multiply a sequence - a list or a tuple or some such - by a numeric value n to repeat that sequence n times:

>>> [1, 2, 3] * 5
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

and so you can do this with strings:

>>> "abc" * 3
'abcabcabc'

but you can't multiply a sequence by another sequence:

>>> [1, 2, 3] * ['a', 'b', 'c']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'list'

and as expected we get the same error when trying to multiply a string by a string:

>>> "abc" * "def"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'

even if both strings look like numbers:

>>> "6" * "10"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'

I hope this helps you understand not just how to fix the error, but what the error was to begin with.

Upvotes: 3

PROTOCOL
PROTOCOL

Reputation: 371

input() reads the given number as a string. You have to convert into a number before doing any arithmetic calculations

a = int(input("What is the length of side a in centimeters"))
b = int(input("What is the length of side b in centimeters"))
area = a * b
print(area)

OR

a = input("What is the length of side a in centimeters")
b = input("What is the length of side b in centimeters")
area = int(a) * int(b)
print(area)

Note: You can simplify the code(if you want):

 a = int(input("What is the length of side a in centimeters"))
 b = int(input("What is the length of side b in centimeters"))
 print(a*b)

Upvotes: 0

Kyle Stuart
Kyle Stuart

Reputation: 111

You need to cast the input to an int/float.

input returns a string so you need to cast it like this:

int(a) * int(b)

Upvotes: 1

Allen Qin
Allen Qin

Reputation: 19957

# Area of rectangle
a = input("What is the length of side a in centimeters")
b = input("What is the length of side b in centimeters")
#convert input to int.
area = int(a) * int(b)
print(area)

Upvotes: 0

Related Questions