Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
'''
Currency Converter
Input: An amount of money, it's ISO 4217 currency code and the ISO 4217 currency code to convert the money too
Output: The converted currency and which country it can be used in
This program is built off using a class object for the currencies, and then using external iterative functions for the calculations
It calls from a text file for useful data of the currencies.
'''
class Currency: # this is the class currency to hold all the important data for us to use for the source and target currency
def __init__(self, country, name, code, rate):
self.country = country # this holds the countries name which the currency can be used in
self.name = name # this holds the currency's name
self.code = code # this holds the currency's ISO 4217 code
self.rate = rate # this holds the currency's GBP conversion rate
# this is the main function of the program where all the user inputs are called and used
def main():
original_currency, target_currency = None, None # presets the variables empty to begin with
loop = True
while loop == True: # this starts a loop
original_amount = input("Please enter the amount of money you would like to convert: ") # asks the user to enter an input
try:
original_amount = round(float(original_amount), 2) # converts the user's input to a float and rounds it to two ddecimal places
loop = False # if no Value Error occurs, then the user has entered a valid amount of money and the loop breaks
except ValueError: # if the value error does occur
print("This is not an amount of money, please try again.") # this lets the user know that what they entered is mot suitable
loop = True # continues the loop so the user can enter another amount of money
original_code = input("Please enter the ISO 4217 currency code of the money you want to convert: ") # user enters their current ISO 4217 code
original_code = original_code.replace(" ","") # this removes all spaces from the string
original_code = original_code.upper() # this makes all the characters are uppercase to match the data in our file
target_code = input("Please enter the ISO 4217 currency code of which you want to convert to: ") # user enters their target ISO 4217 code
target_code = target_code.replace(" ", "") # this removes all spaces from the string
target_code = target_code.upper() # this makes all the characters are uppercase to match the data in our file
file = open("Country_Codes.txt", mode="r") # this opens the text file we're using in read mode
for line in file: # this loops through each line in the file, this is currently fine as it is only a small text file
country, name, code, rate = line.split(", ") # this splits the string and creates new variables
if original_code == code: # this checks to see if the original code the user gets matches the line's code
original_currency = Currency(country, name, code, float(rate)) # this creates the new instance of the Currency class for the original currency
if target_code == code: # this checks to see if the target code the user gets matches the line's code
target_currency = Currency(country, name, code, float(rate)) # this creates the new instance of the Currency class for the target currency
if original_currency == None or target_currency == None: # this checks if the codes have been found
print("Your currency was not found, try enter the infomation again") # if they haven't been found, it notifies the user
main() # starts the program again
else: # if they have been found, the program continues
GBP = float(original_amount) / original_currency.rate # converts original currency to british sterling
conversion = round(GBP * target_currency.rate, 2) # then converts the british sterling to the new curency, and rounds it up to two decimal places
print("The value of your money will be " + str(conversion) + " " + target_currency.name + " in " + target_currency.country) # outputs to the user the country, name of the currency and the amount of money they would have
main()