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

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.

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.

IS IT YOUR FIRST TIME HERE? WELCOME

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