program5_1.py Write a program that displays a table of ten distance equivalents in miles and kilometers. See Example output. You must generate the table by running a function inside a loop in main. Generate a random integer from 10 to 60, inclusive, in each loop cycle. Use this latter value as the miles argument to the function. Repeat: The function prints the table. Print the miles in a column 5 characters wide with 2 decimals and the kilometers in a column 13 characters wide with 5 decimals. Use the column formatting concepts at the end of Chapter 2, not tabs or other methods not in this course. Example output
program5_1.py
Write a program that displays a table of ten distance equivalents in miles and kilometers. See Example output. You must generate the table by running a function inside a loop in main. Generate a random integer from 10 to 60, inclusive, in each loop cycle. Use this latter value as the miles argument to the function. Repeat: The function prints the table. Print the miles in a column 5 characters wide with 2 decimals and the kilometers in a column 13 characters wide with 5 decimals. Use the column formatting concepts at the end of Chapter 2, not tabs or other methods not in this course.
Example output
CODE IN PYTHON:
import random
def toKilometers(miles):
return miles * 1.60934
print("MILES KILOMETERS")
for i in range(10):
miles = random.randrange(10, 61) * 1.0
km = toKilometers(miles)
print("{0:.2f} {1:.5f}".format(miles, km))
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images