+ 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")
3 Respostas
+ 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}")
+ 3
Thanks guys ❤
You are amazing 👏
+ 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")