Using the format method, fill in the gaps in the convert_distance function so that it returns the phrase "X miles equals Y km", with Y having only 1 decimal place. For example, convert_distance(12) should return "12 miles equals 19.2 km".
1 an AWN def convert_distance (miles): km = miles * 1.6 result = "{} miles equals {_} km". _ return result 7 8 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

Answer :

mahamnasir

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.

${teks-lihat-gambar} mahamnasir
MrRoyal

The format method is used to substitute and format output data before they are eventually printed.

The complete statement using the format method is:

result = "{} miles equals {:.1f} km".format(miles,km)

From the question, we understand that the program is to output the converted distance to 1 decimal place.

The function definition receives miles as its argument, while the equivalent distance in kilometers is calculated within the function.

So,

  • The first blank will be left empty, to output miles unformatted
  • The second blank will be filled with :.1 , to output km formatted to 1 decimal place
  • The third blank will be filled with the output variables i.e. format(miles,km)

Read more about format methods at:

https://brainly.com/question/19554596

Other Questions