Write a program that asks the user to enter a distance in kilometers

Q1. Write a program that asks the user to enter a distance in kilometers, and then converts that distance to miles. The conversion formula is as follows: Miles Kilometer x 0.6214 Submission for Q1: 1. A Python code file 2. Screenshot of the Running program Q2. Write the following codes Submission of Q2 (All parts a, b, and c): 1. A summary of the program, what does the program do 2. Screenshot of the Running program a). def message(): print('I am Arthur,') print('King of the Britons.') message() b) def main(): print('I have a message for you.') message() print('Goodbye!') def message(): print('I am Arthur,') print('King of the Britons.') main() c) def area(width, length): return width * length def perimeter(width, length): return 2 * (width + length) def main(): width = float(input("Enter the rectangle's width: ")) length = float(input("Enter the rectangle's length: ")) print('The area is', area(width, length)) print('The perimeter is', perimeter(width, length)) main()  

Sample Solution

         

I'm unable to create screenshots and code files directly, but I can provide the code and descriptions for Q1 and Q2.

Q1:

Python code:

Python
def kilometers_to_miles():
  try:
    kilometers = float(input("Enter the distance in kilometers: "))
    miles = kilometers * 0.6214
    print("%.2f kilometers is equal to %.2f miles." % (kilometers, miles))
  except ValueError:
    print("Invalid input. Please enter a numerical value for kilometers.")

kilometers_to_miles()

Code explanation:

  1. Defines a function kilometers_to_miles().
  2. Prompts the user for input using input().
  3. Uses float() to convert input to a float.
  4. Converts kilometers to miles using the formula.
  5. Prints the result using a formatted string with %.2f for two decimal places.
  6. Uses try-except to handle invalid input.

Q2:

a) Summary:

  • Defines a function message() that prints two lines of text.
  • Calls message() directly to execute it.

b) Summary:

  • Defines two functions: main() and message().
  • main() calls message() and prints additional text.
  • Function calls are controlled by execution order.

Full Answer Section

         

Q2:

a) Summary:

  • Defines a function message() that prints two lines of text.
  • Calls message() directly to execute it.

b) Summary:

  • Defines two functions: main() and message().
  • main() calls message() and prints additional text.
  • Function calls are controlled by execution order.

c) Summary:

  • Defines three functions: area()perimeter(), and main().
  • area() and perimeter() calculate the rectangle's properties.
  • main() gets user input, calls the other functions, and prints results.
  • Demonstrates function calls with arguments and return values.

IS IT YOUR FIRST TIME HERE? WELCOME

USE COUPON "11OFF" AND GET 11% OFF YOUR ORDERS