Python Forum
Using Data from an Array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Data from an Array
#1
G,Day everyone :)

so my class has been given a assignment to make a currency converter. what we are asked to do is. read data from csv file and get a specific set of exchange rates from a certain date listed in the csv file. made using a function so we then return an array that has to look like "unit,rate,unit,rate" unit = currency code "HKD code = Hong Kong Dollor" rate is the rate at which the useramount is exchanged by "useramount = float of "numbers" and rate is gathered from returned array. output will end up being useramount * rate. also the converter is split across 2 python files also. reading and converting in one file and getting the user input data and asking if they would like to convert again the the 2nd python file. just want to note that we are not to use the csv module.
I've read the csv and got the array returned as it needs to be, building of the array could be better but unsure of how to do that, some advice for that would be great(thats not my main problem). my main problem is trying to get the rate i need from the returned array to be used in the conversion. i want to able to check if the usercode == "HKD" in array and +1 to "HKD" position(as this is where the exchange rate for that currency code lives) and set to a variable to be used in the conversion of the inputed useramount.

i hope all of the above makes sense, obviously i'm looking not for the answer i'm looking for something to get me on the right track. Thanks in advance for taking the time to read all this and i really do appreciate any advice or suggestions.

all the code i have so far is listed below.

#Begging of finance.py - python file 1 
def read_file(): 
    #Reading in csv file as "csvfile"
    with open("f11hist.csv", "r") as csvfile:
        #Iterating over the entire csvfile a line at a time
        for line in csvfile:
            #Checking for 28-Sep-2018 in line for later use 
            if "28-Sep-2018" in line:
                #splitting line into separate values and creating a array
                date_data = line.split(",")
                #Breaking the loop
                break
        #creating a temporary list of the currency codes    
        x = "JPY GBP NZD HKD CAD"
        #splitting the temporary list into separate values
        currency_codes = x.split(" ")
        #example of list outcome
        """
        ["JPY", "GBP", "NZD", "HKD", "CAD"] - currency_codes
        ["81.96","0.5519","1.0921","5.6525","0.9405"] - date_data
        """
        
        #end result of list for the exchanging currencies
        exchange = [
            currency_codes[0], date-data[4],  # JPY & Exchange Rate
            currency_codes[1], date-data[7],  # GBP & Exchange Rate
            currency_codes[2], date-data[11], # NZD & Exchange Rate
            currency_codes[3], date-data[18], # HKD & Exchange Rate
            currency_codes[4], date-data[19]  # CAD & Exchange Rate
        ];
        #returns the list for use
        return exchange

def currency_convert(amount, rate):
    return amount * rate

#Begging of Running_Converter.py - python file 2
from finance import read_file
from finance import currency_convert

def user_convert(): 
    print("""
##########################
### Currency Converter ###
##########################
Please select one of the
following currency codes
-JPY-
-GBP-
-NZD-
-HKD-
-CAD-""")
    usercode = input("Please enter a Country code: ")
    useramount = float(input("Please enter the amount you wish to: $"))
    for line in read_file():
        if line in usercode:
            print(line)
Reply
#2
Following saying is attributed to Socrates: "The beginning of wisdom is the definition of terms."

There is no array built-in in Python. In Python there are arrays in NumPy and arrays can be created using built-in module array but neither of them is used in your code.

It may seem nitpicking but if your assignment says to use arrays you should use arrays, not lists. If your teacher meant lists, not arrays he/she should stand corrected.

Easy reading about array module can be found here).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie here. Create an array from file data? Rayj00 2 1,198 Jan-13-2023, 01:35 PM
Last Post: perfringo
  Store data in array kasper1903 4 3,028 Oct-04-2019, 12:43 PM
Last Post: kasper1903
  Call out data from 2D array to perform calculation poonck1 1 3,258 Feb-28-2017, 01:53 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020