我从instagram上的Python\u脚本得到的简单货币转换程序出错

2024-04-23 12:07:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在用python转换货币的代码中遇到这个烦人的错误。你知道吗

这段代码来自instagram上的python脚本。所有的东西都在检查,但这个小虫子正在阻止节目。我做错了什么。你知道吗

这就是弹出的错误。你知道吗

文件“C:/Users/Pig Toy's PC/Documents/PythonPrograms/电流转换器.py“,第70行” +“&;to\u currency=”toCur+“&;apikey=”+apikey ^ 语法错误:无效语法

我运行了所有其他函数,它们都检查出来了。你知道吗

"""
Created on Sun May 19 17:15:47 2019

@author: ArtOfDHT
"""

#real time currency conversion.

#importing necessary packages
import requests 
import tkinter as tk 
from tkinter import *
from tkinter import ttk

#list of currencies

currencylist = [ 'AED', 'AUD', 'BHD', 'BRL', 'CAD',
                'CNY' , 'EUR', 'KHD' , 'INR' , 'USD' ]

#defining inputs and widgets for GUI

def CreateWidgets():
    inputAMTL = label(root, text="Enter The Amount:", bg="SpringGreen4")
    inputAMTL.grid(row=1, column=2, columnspan=2, pady=10)

    inputAMTxt = Entry(root, width=20, textvariable=getAMT)
    inputAMTxt.grid(row=1, column=3, columnspan=2, pady=10)    

    fromLabel = Label(root, text="FROM:", bg="SpringGreen4")
    fromLabel.grid(row=2, column=1)

    root.fromCombo = ttk.Combobox(root, values=currencyList)
    root.fromCombo.grid(row=2, column=2)

    toLabel = Label(root, text="TO:", bg="SpringGreen4")
    toLabel.grid(row=2, column=3)

    root.toCombo = ttk.Combobox(root, values=currencyList)
    root.toCombo.grid(row=2, column=4)

    convertButton = button(root, text="Convert", width=52, command=Convert)
    convertButton.grid(row=3, column=1, columnspan=2, pady=50)

    outputANTL = label(root, text ="Converted Amount:", font=('Helvetica', 10),bg="SpringGreen4")
    outputANTL.grid(row=4, column=2, columnspan=2, pady=50)

    root.outputAMTAns = Label(root, font=("Helvetica", 20),bg="SpringGreen4")
    root.outputAMTans.grid(row=4, column=3, columnspan=2, pady=50)

def Convert():
    #fetch and storing user-input in responsive variables 
    inputAmt = float(getAmt.get())
    fromCur = root.fromCombo.get()
    toCur = root.toCombo.get()

    #storing API key

    apikey ="FP6UEQWASCREGU57"

    #storing the base URL

    baseURL = r"http://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE"

    #storing the inputs
    inputURL = baseURL + "&to_currency=" + toCur + "&apiKey" + apiKey

    #return requests 
    inputURL = baseURL + "&from_currency=" + fromCur\
                + "&to_currency=" toCur + "&apikey=" + apiKey

    #response return
    requestObj = requests.get(inputURL)

    #converting the json format data to Python 
    result = requestObj.json()

    #getting the exchange rate 
    exchangeRate = float(result["Realtime Currency Exchange Rate"]
                                              ['5, Exchange Rate']

    #calculate the converted amount and rounding to nearest 2 places
    calculateAmt = round(inputAmt * exchangeRate, 2)

    #Display the converted amount in the respected label 
    root.outputAMTAns.config(text=str(calculateAmt)) 

    #creating root class 
    root = tk.Tk()

    #setting gui structure
    root.geometry("400x250")
    root.resizable(False, False)
    root.title("PyCurrencyConverter")
    root.config(bg = "SpringGreen4")

    #Create tkinter variable 
    getAMT = StringVar()

    #Calling the CreateWidgets() function
    CreateWidgets()

    #defining infinite loop to run app
    root.mainloop() ~~~~



This is the error that pops up.


 File "C:/Users/Pig Toy's PC/Documents/PythonPrograms/currencyconverter.py", line 70
    + "&to_currency=" toCur + "&apikey=" + apiKey

I ran all the other functions and they check out. 


Tags: thetotextimportcolumnrootcurrencygrid
2条回答

您在第69行的"&to_currency="toCur之间缺少一个+符号(假设您提供了此文件中的所有源代码)

# return requests 
inputURL = baseURL + "&from_currency=" + fromCur\
           + "&to_currency=" + toCur + "&apikey=" + apiKey

您的字符串连接有问题

import urllib

# Python 3:
import urllib.parse

getVars = {'function': 'CURRENCY_EXCHANGE_RATE', 'from_currency': 'fromCur', 'to_currency': toCur, 'apiKey': apiKey}
url = "http://www.alphavantage.co/query?"

# Python 2:
inputURL = url + urllib.urlencode(getVars)

# Python 3:
inputURL = url + urllib.parse.urlencode(getVars)

https://docs.python.org/3/library/urllib.html

或使用格式https://pyformat.info/

inputURL = "{}&to_currency={}&apiKey={}".format(baseURL, toCur, apiKey)

相关问题 更多 >