GST Calculator
Some countries charge GST (goods and services tax), which is paid on most services and purchased items. This tax is:
- 10% in Australia
- 15% in New Zealand
- 17% in China.
Write a program that prompts the user for the amount of their purchase and the percentage the country charges for GST. The program should print GST amount being charged on their purchase. For example, if the user inputs $100
and 10%
, the program should output $10.00.
Warning
Symbols like ¥ and £ are non ASCII characters and should not be used in this program. Only use ASCII characters for now. We will cover internationalisation (I18n) in a later lesson.
def main():
purchase = input("How much was the purchase? ")
percentage = input("What percentage is the GST rate? ")
gst = calculate_gst(purchase, percentage)
print(f"GST will be ${gst:.2f}")
def calculate_gst(purchase, percentage):
price = currency_to_float(purchase)
percent = percent_to_float(percentage)
gst = price * percent
return gst
def currency_to_float(d):
# TODO
def percent_to_float(p):
# TODO
main()
Well, we’ve written most of a GST calculator for you. Unfortunately, we didn’t have time to implement two functions:
currency_to_float
, which should accept astr
as input (formatted as$##.##,
wherein each#
is a decimal digit), remove the leading$
, and return the amount as afloat
. For instance, given$50.00
as input, it should return50.0
.percent_to_float
, which should accept astr
as input (formatted as##%,
wherein each#
is a decimal digit), remove the trailing%
, and return the percentage as afloat
. For instance, given15%
as input, it should return0.15
.
Assume that the user will input values in the expected formats.
Tip
- Recall that
input
returns astr
, per input. - Recall that a
str
comes with quite a few methods, per string-methods. - Recall that
float
can convert astr
to afloat
, per float.
Before You Begin
- Ensure you are in the root directory of the repository that you cloned to your machine.
- Change directory to
src/function_variables/
in your terminal window. - create or open the file
gst.py
This is where you’ll write your program.
How to Test
Here’s how to test your code manually:
Run your program with python gst.py. Type $50.00 and press Enter. Then, type 15% and press Enter. Your program should output:
Run your program with python gst.py. Type $100.00 and press Enter. Then, type 18% and press Enter. Your program should output: Run your program with python gst.py. Type ¥15.00 and press Enter. Then, type 17% and press Enter. Your program should output:You can execute the below to check your code using pytest
from the root directory.
A green output from running the test means it was successful. A red output means there is a bug in your code that you need to fix.
How to Submit
From github desktop or the command line, commit your changes and push them to your repository.
Codespaces
If you are using codespaces, you can commit your changes directly from the Codespace interface. Click on the Source Control icon in the left sidebar, then click on the "..." button and select "Commit to main". Enter a commit message and click "Commit".
Codespace terminal or your local terminal.
Note
You will need to have installed git-scm
for this to work locally
At the /functions_variables $
prompt in your terminal:
Note
Remember to replace "your message here" with a meaningful commit message that describes your changes.