Answer:
Add this statement to the code using format method:
result = "{} miles equals {:.1f} km".format(miles,km)
Explanation:
Here is the complete program:
def convert_distance (miles):
km = miles * 1.6
result = "{} miles equals {:.1f} km".format(miles,km)
return result
print(convert_distance(12))# should be: 12 miles equals 19.2 km
print(convert_distance(5.5)) # should be: 5.5 miles equals 8.8 km
print(convert_distance(11)) # Should be: 11 miles equals 17.6 km
The format() method is used to format the values of the specified parameters and insert the formatted values into the curly brackets which are called placeholders. The parameters of format() method here are miles and km
. The first placeholder is for formatted value of miles variable and second placeholder is for formatted value of km variable. Note that placeholder for km has {:.1f} which means the value of km is rounded to 1 decimal place. For example if the value is 19.23245 then it is displayed up to 1 decimal place as: 19.2
The above program has a method convert_distance that takes miles as parameter and converts the value of miles to km by formula:
km = miles * 1.6
It then displays the output in the specified format using format() method. Now lets take an example:
print(convert_distance(5.5))
The above statement calls the method by passing the value 5.5 So,
miles = 5.5
Now the function converts this value to km as:
km = miles * 1.6
km = 5.5 * 1.6
km = 8.8
Statement: result = "{} miles equals {:.1f} km".format(miles,km) becomes:
{} first place holder holds the value of miles i.e. 5.5
{} second place holder holds the value of km up to 1 decimal place i.e. 8.8
Hence it becomes:
5.5 miles equals 8.8 km
So return result returns the result of this conversion.
Hence the output is:
5.5 miles equals 8.8 km
The program and its output is attached.