user3425024
user3425024

Reputation: 1

Why is this string multiplication throwing an error?

x = 0.8
y = str(x)
x=x*y
print x, y

Just beginning Python, not looking to fix the code, rather work out how to justify why there is an error. I believe that this attempts to pass a string off as an integer just not sure why you can't do that.

Upvotes: 0

Views: 216

Answers (1)

Christian Tapia
Christian Tapia

Reputation: 34166

You have this:

x = 0.8      # float
y = str(0.8)

the last line, will be equivalent to

y = "0.8"    # string

then, when you do

x = x * y    # float * string

you get an error, because it's not possible to multiply a string with a float. But note that you can do that with integers:

x = 3
y = str(6)   # y = "6"

this will produce the output "666", because it's multiplying the string "6" 3 times.

Upvotes: 2

Related Questions