write a program to convert kilometres Into miles
write program in python
plz answer my question
igs urgent python question
Answers
km = float(input("Enter the measure in Kilometers: "))
m = km/1.609
print(km, "kilometers is equivalent to", round(m, 2), "miles.")
We're using float() since measurements [especially relating to distances] could always have the possibility of being in decimals. If we were to use int() as the data type and a user enters a decimal value, the program would result in an error. To be on the safer side, we use float() as both integers and decimals are accepted.
We then store the conversion into a separate variable and use it in the next print statement.
Since the final division is also in the possibility of being a really huge number, i.e., having more than 4 decimal places, we use the round() function that rounds the resulting decimal values according to the number we require. In this case, I've limited it to 2 decimal places.
You could also directly put the conversion in the print statement and remove the round() function if you want all the decimal places to be printed.
I'll be pasting the same program below with the above-mentioned suggestions.
km = float(input("Enter the measure in Kilometers: "))
print(km, "kilometers is equivalent to", km/1.609 "miles.")