What this error means
You tried to multiply a sequence (string, list, tuple) by a float (decimal number). Sequences can only be multiplied by integers. Multiplying a string by 2 gives "hellohello", but multiplying by 2.5 doesn't make sense.
Common causes
- You have a float when you expected an integer.
- User input or calculation resulted in a float.
- Confusion between sequence multiplication and numeric multiplication.
Example that causes the error
text = "hello"
result = text * 2.5 # TypeError: can't multiply by float
items = [1, 2, 3]
result = items * 1.5 # TypeError: can't multiply by float
How to fix it: convert to int
Convert the float to an integer first:
text = "hello"
multiplier = 2.5
result = text * int(multiplier) # OK: converts 2.5 to 2
print(result) # "hellohello"
Use round() if appropriate
text = "hello"
multiplier = 2.7
result = text * round(multiplier) # OK: rounds to 3
print(result) # "hellohellohello"
Check the type first
multiplier = some_calculation()
if isinstance(multiplier, int):
result = text * multiplier
elif isinstance(multiplier, float):
result = text * int(multiplier)
else:
print("Invalid multiplier type")