For循环不会遍历字典

时间:2020-08-14 14:09:19

标签: python json python-requests

所以我的代码在下面

import requests
import json

with open("forex_rates.json", "r") as file:
    rate = json.load(file)
    nrate = rate

Base = input("enter your base currency: ")
if Base not in rate["rates"]:
    print("That's not a valid currency...")
    Base = input("enter your base currency: ")

with open("forex_rates.json", "r") as file:
    nrate["base"] = Base
    for i in rate["rates"]:
        nrate["rates"][i] = rate["rates"].get(i) / rate["rates"].get(Base)
        
print(nrate["rates"])

我正在尝试制作一个可以转换不同货币的程序,例如: 1欧元-> 1.18美元 为了做到这一点,我必须根据基准值每次编辑外汇汇率 当我完成代码后,我运行了它,一切正常,它转换了所有值,直到在外汇价值文件中达到相同的值 就像如果我从USD转换,它将根据USD更改所有货币值,但是当文件中的USD达到USD时,循环中断 我尝试了很长时间,直到尝试将USD移到文件的最底部为止,因为它没有损坏的余地。确实有效,但这仅适用于美元。 当我在程序中更改基本值时,它将再次中断

对于那些说代码甚至还没有完成的人,我会说我几乎将程序的这一部分完成了转换部分,我认为最困难的部分,剩下的任何其他代码只是GUI和一些乘法运算

,我将在下面列出外汇数据 #对不起,如果太大的话

{
  "success":true,
  "timestamp":1597402446,
  "base":"EUR",
  "date":"2020-08-14",
  "rates":{
    "AED":4.335266,
    "AFN":91.176403,
    "ALL":123.979569,
    "AMD":572.651268,
    "ANG":2.125143,
    "AOA":695.22591,
    "ARS":86.213707,
    "AUD":1.651881,
    "AWG":2.124558,
    "AZN":2.001576,
    "BAM":1.954795,
    "BBD":2.390258,
    "BDT":100.546318,
    "BGN":1.956895,
    "BHD":0.444938,
    "BIF":2283.56194,
    "BMD":1.18031,
    "BND":1.622912,
    "BOB":8.173103,
    "BRL":6.336422,
    "BSD":1.183874,
    "BTC":0.0001,
    "BTN":88.576746,
    "BWP":13.863097,
    "BYN":2.914727,
    "BYR":23134.077322,
    "BZD":2.38624,
    "CAD":1.562064,
    "CDF":2302.785095,
    "CHF":1.074967,
    "CLF":0.033942,
    "CLP":936.574514,
    "CNY":8.203627,
    "COP":4452.129574,
    "CRC":704.672423,
    "CUC":1.18031,
    "CUP":31.278217,
    "CVE":110.201987,
    "CZK":26.106129,
    "DJF":210.746982,
    "DKK":7.446693,
    "DOP":69.149105,
    "DZD":151.514114,
    "EGP":18.805169,
    "ERN":17.704664,
    "ETB":42.448715,
    "EUR":1,
    "FJD":2.523208,
    "FKP":0.90119,
    "GBP":0.900976,
    "GEL":3.647475,
    "GGP":0.90119,
    "GHS":6.811663,
    "GIP":0.90119,
    "GMD":61.163652,
    "GNF":11418.288925,
    "GTQ":9.116687,
    "GYD":247.470583,
    "HKD":9.14817,
    "HNL":29.203723,
    "HRK":7.531326,
    "HTG":132.978265,
    "HUF":346.161502,
    "IDR":17595.177253,
    "ILS":4.019203,
    "IMP":0.90119,
    "INR":88.434024,
    "IQD":1413.229387,
    "IRR":49696.95603,
    "ISK":161.076893,
    "JEP":0.90119,
    "JMD":176.632894,
    "JOD":0.836814,
    "JPY":125.951475,
    "KES":127.822789,
    "KGS":92.238283,
    "KHR":4859.521032,
    "KMF":491.421896,
    "KPW":1062.314938,
    "KRW":1401.210938,
    "KWD":0.360975,
    "KYD":0.986486,
    "KZT":496.331879,
    "LAK":10752.381531,
    "LBP":1789.879783,
    "LKR":216.488093,
    "LRD":235.294815,
    "LSL":20.549263,
    "LTL":3.485149,
    "LVL":0.713958,
    "LYD":1.626859,
    "MAD":10.913322,
    "MDL":19.82872,
    "MGA":4566.909302,
    "MKD":61.58448,
    "MMK":1610.391686,
    "MNT":3362.205621,
    "MOP":9.449856,
    "MRO":421.37087,
    "MUR":46.799002,
    "MVR":18.243058,
    "MWK":879.737053,
    "MXN":26.199071,
    "MYR":4.949633,
    "MZN":83.979304,
    "NAD":20.548996,
    "NGN":449.107914,
    "NIO":41.242296,
    "NOK":10.544902,
    "NPR":141.752962,
    "NZD":1.80663,
    "OMR":0.454403,
    "PAB":1.183663,
    "PEN":4.2222,
    "PGK":4.165565,
    "PHP":57.516411,
    "PKR":199.282277,
    "PLN":4.396536,
    "PYG":8227.988848,
    "QAR":4.297547,
    "RON":4.835722,
    "RSD":117.576625,
    "RUB":86.625365,
    "RWF":1142.595148,
    "SAR":4.426709,
    "SBD":9.756128,
    "SCR":21.044237,
    "SDG":65.300702,
    "SEK":10.291595,
    "SGD":1.620105,
    "SHP":0.90119,
    "SLL":11537.53114,
    "SOS":689.301231,
    "SRD":8.802808,
    "STD":25132.067341,
    "SVC":10.358743,
    "SYP":603.981109,
    "SZL":20.627972,
    "THB":36.753085,
    "TJS":12.208818,
    "TMT":4.131085,
    "TND":3.240537,
    "TOP":2.698542,
    "TRY":8.706498,
    "TTD":8.006077,
    "TWD":34.707966,
    "TZS":2744.220413,
    "UAH":32.433598,
    "UGX":4343.852885,
    "USD":1.18031,
    "UYU":50.261259,
    "UZS":12111.300903,
    "VEF":11.788346,
    "VND":27353.095658,
    "VUV":134.283098,
    "WST":3.09309,
    "XAF":655.754247,
    "XAG":0.044343,
    "XAU":0.000606,
    "XCD":3.189847,
    "XDR":0.840933,
    "XOF":655.720927,
    "XPF":119.624668,
    "YER":295.426891,
    "ZAR":20.617975,
    "ZMK":10624.212769,
    "ZMW":21.85909,
    "ZWL":380.05997
  }
}

0 个答案:

没有答案