235. Python Program to Convert Money with Fixed Exchange Rates
EasyPhase 1 · Warm-up
You have used input(), float() and if with elif before. They are all you need here.
Overview
A currency converter changes money from one kind to another. An exchange rate says how much one unit of a currency is worth.
Problem Statement
Fatima runs a shop in a space game. Players pay with three game currencies, and the shop counts everything in coins. The fixed exchange rates are:
- 1 star (code
STAR) is worth 2.5 coins. - 1 moon (code
MOON) is worth 0.4 coins. - 1 sun (code
SUN) is worth 12 coins.
Read a currency code and an amount. Print the value in coins with 2 decimal places, as Coins: <coins>. If the code is not one of the three, print Unknown currency instead.
Steps
- Read the code as text, and read the amount as a decimal number.
- Check which of the three codes it is.
- Multiply the amount by that code's rate.
- Print the coins with 2 decimal places, or
Unknown currencywhen no code matches.
Examples
Example 1. Input:
STAR
10
gives:
Coins: 25.00
Why: 1 star is worth 2.5 coins, so 10 stars are 10 × 2.5 = 25 coins.
Example 2. Input:
GEM
10
gives:
Unknown currency
Why: GEM is not STAR, MOON or SUN, so there is no rate for it.
Input
Two lines:
- The currency code in capital letters, like
STAR. - The amount, a number of 0 or more (it may have a decimal point).
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: Coins: <coins> with 2 decimal places, or Unknown currency. The tests try 6 different inputs, also an amount of 0.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.