Advertisement
timber101

Arrays

Jan 3rd, 2022
902
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None
  1. # arrays
  2.  
  3. # list or table of related items
  4.  
  5. # 1 dimensional array = > list
  6.  
  7. # index    0  1  2  3  4   always starts at zero 0
  8. scores = [45,52,63,24,34]
  9.  
  10. print (len(scores))
  11.  
  12. print("1st item",scores[0])
  13.  
  14. print("last item using direct index",scores[4])
  15. print("last item using length index",scores[(len(scores))-1]) # if you see a -1 in an exam be careful
  16. print("last item using direct index from the other end",scores[-1])
  17.  
  18. # 2 dimensional array
  19.  
  20. # currency exchange rates
  21. # https://www.exchangerates.org.uk/
  22.  
  23.  
  24. """
  25.       0          1
  26. 0 GBP EUR       1.1897  Pounds to Euros
  27. 1 GBP USD       1.3475  Pounds to Dollars
  28. 2 GBP NZD       1.9846  Pounds to New Zealand Dollars
  29.  
  30. """
  31.  
  32. # array = list of lists in python
  33.  
  34. #  list                   0                1               2
  35. #  item                0       1       0       1       0       1
  36. exchange_rates = [["EUR", 1.1897],["USD", 1.3475],["NZD", 1.9846]]
  37.  
  38. print(len(exchange_rates))
  39.  
  40. print("1st item",exchange_rates[0])
  41. print("1st item",exchange_rates[0][1])
  42.  
  43. cash_held = float(input("how much cash have you got >> "))  # need to be a float for decimal amount
  44. buy_currency = input("what currency do you want to buy >> ") # enter the code
  45. currency_amount = 0 # asign exchaged amount
  46.  
  47. for each_item_index in range(len(exchange_rates)):
  48.     #print(each_item_index) testing
  49.     if exchange_rates[each_item_index][0] == buy_currency:
  50.         currency_amount = cash_held * exchange_rates[each_item_index][1]
  51.  
  52. print(str(cash_held) + " would buy you " + str(currency_amount) + " " + buy_currency ) # easier way to do this line
  53.  
  54. print(cash_held," would buy you ",currency_amount,buy_currency ) # easier way to do this line
  55.  
  56.  
  57. """
  58. EXTENSION IDEAS
  59. 1.  add more currencies
  60. 2.  add to convert the other way say you had dollars and wanted GBP ( multiply by 1/ rate)
  61. 3.  add a while loop so keeps going until a key word ("Quit" say) is entered for the country code
  62. 4.  export to an offline file, as a data backup extension
  63.  
  64.  
  65.  
Advertisement
RAW Paste Data Copied
Advertisement