livepython.net

240. Python Program to Make a Unit Converter Menu

EasyPhase 1 · Warm-up

You know input(), if with elif, and f-strings with :.2f. That is all you need here.

Overview

Many programs start with a menu: you type a number to pick what the program does. A unit converter is a good first menu, because each choice is one small sum.

Problem Statement

Elena travels a lot and keeps mixing up units. Elena wants a converter with 3 choices:

  • 1: kilometres to miles. Multiply by 0.62.
  • 2: kilograms to pounds. Multiply by 2.2.
  • 3: degrees Celsius to degrees Fahrenheit. Multiply by 9, divide by 5, then add 32.

The program does not print the menu. It reads the choice and then the value. Print the value and the result, both with 2 decimal places, in one of these formats:

<value> km = <result> miles
<value> kg = <result> pounds
<value> C = <result> F

If the choice is not 1, 2 or 3, print Invalid choice instead.

Steps

  1. Read the choice as a whole number, and the value as a decimal number.
  2. Check the choice: 1, 2 or 3.
  3. Use that choice's formula on the value.
  4. Print the line with 2 decimal places, or Invalid choice when the choice is not 1, 2 or 3.

Examples

Example 1. Input:

1
10

gives:

10.00 km = 6.20 miles

Why: choice 1 is km to miles, and 10 × 0.62 = 6.2.

Example 2. Input:

3
100

gives:

100.00 C = 212.00 F

Why: choice 3 is Celsius to Fahrenheit. 100 × 9 = 900, 900 ÷ 5 = 180, and 180 + 32 = 212.

Input

Two lines:

  1. The choice, a whole number.
  2. The value to convert, a number that may have a decimal point or a minus sign.

Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line in the format of the choice, with 2 decimal places, or Invalid choice. The tests try 6 different inputs, also a temperature below 0 and a wrong choice.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 8 · If Statements
solution.py
# write your code here

EXPECTED OUTPUT

110.00 km = 6.20 miles

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.