+ 1

What's wrong with this code?

Trying to make a mile_to_kilometer converter, I wrote this code. However, it doesn't work. Here's the code: mile = int(input("Enter Mile: ")) kilometer = mile * 1.609344 print(mile + "mile is equal to " + kilometer + "kilometer")

29th Aug 2021, 11:34 AM
Ali K. Salemi
Ali K. Salemi - avatar
3 Answers
+ 3
You need to convert the integer, float or pretty much any data type to string if you want to use + to concatenate things. Use str() function to convert to str. Or you can use string formatting. Example, print(f"{mile} mile is equal to kilometer {kilometer}")
29th Aug 2021, 11:38 AM
Abhay
Abhay - avatar
+ 3
Thanks guys ❀ You are amazing 👏
29th Aug 2021, 1:41 PM
Ali K. Salemi
Ali K. Salemi - avatar
+ 2
Hi Ali! Similarly, you can also use comma as concatenation operator. For that, you don't have to convert the data types of the variable as we are doing it for "+" operator. Also, it will add a space between strings and variables by default. print(mile, "mile is equal to", kilometer, "kilometer")
29th Aug 2021, 11:50 AM
Python Learner
Python Learner - avatar