# Percentages With Python Python is great for maths, and as home schooling continues here in the UK due to the Coronavirus lockdown I’m using the time to look at converting percentages (%) using Python. This is the second of my Maths and Python posts, with fractions being my first.

I’m trying to include more f string usage in my Python programmes as this became an option of the Python print statement with Python version 3.6 and thanks to “Beyond The Basic Stuff With Python” by Al Sweigart (No Starch Press – see: https://nostarch.com/beyond-basic-stuff-python) I’m more aware of it. The below code includes several examples.

``````# geektechstuff percentages
# Requires Python3.6 or above for f' string usage

def main():
# welcomes user
print("Welcome to GeekTechStuff Percentage Calculator")
print("This percentage calculator has the following options: ")
print("1) Turn a percentage into an amount, e.g. 25% of 100 is 25")
print("2) Turn an amount into a percentage, e.g. 50 of 100 is 50%")
print("3) Find out the original amount after a percentage has been added, e.g. How much an item cost before 20% was added")
print("4) Find out the original amount after a percentage was removed, e.g. How much was an item before 20% was removed")
user_selection = input("Please choose an option using the numbers 1, 2, 3 or 4: ")
choice(user_selection)

def choice(user_selection):
# turns input choice into appropriate function
try:
user_selection = int(user_selection)
except:
error_msg(input)
if user_selection == 1:
return percentage_into_amount()
if user_selection == 2:
return amount_into_percentage()
if user_selection == 3:
if user_selection == 4:
return percentage_removed()
else:
return error_msg(user_selection)

def error_msg(input):
# displays error if user input out of expected range
print(f"Sorry I did not understand {input}, does it correspond to the required input options/choices?")
return

def percentage_into_amount():
# converts an percentage into an amount
#
percent = input("What is the percentage %: ")
try:
percent = int(percent)
except:
error_msg(percent)
amount = input("What is the amount: ")
try:
amount = int(amount)
except:
error_msg(amount)
# formula is amount divided by 100 multiplied by percentage

def amount_into_percentage():
# converts an amount into a percentage
amount = input("What is the amount: ")
try:
amount = float(amount)
except:
error_msg(amount)
total_amount = input("What was the total figure that you are using the amount from? ")
try:
total_amount = float(total_amount)
except:
error_msg(total_amount)
# formula is amount divided by total_amount, multiplied by 100 equals percent

# Turns an amount with a percentage added, back to original amount (i.e. minus the percentage)
current_amount = input("What is the current amount?: ")
try:
current_amount = float(current_amount)
except:
error_msg(current_amount)
try:
except:
# divide current amount by the difference of the percent added and 100%
# e.g. 75% if 25% were added, then multiply by 100
return(print(f"If the amount {current_amount} includes {percentage_added}%, then the original amount was {answer}"))

def percentage_removed():
# Turns an amount with a percentage removed, back to original amount (i.e. minus the percentage)
current_amount = input("What is the current amount?: ")
try:
current_amount = float(current_amount)
except:
error_msg(current_amount)
percentage_removed = input("What percentage was removed?: ")
try:
percentage_removed = float(percentage_removed)
except:
error_msg(percentage_removed)
# divide current amount by the difference of the percent removed and 100%
# e.g. 75% if 25% were added, then multiply by 100