In-and-out


#1

I’m having a little problem with functions in python, more specifically understanding (and finding) the part covering how to adopt something into the function and how to get the variable out again (once altered inside the function). I’ll paste my code below and hope that someone can assist with it however I’m also hoping that someone can link me to the specific exercises that cover these topics.

#Program to get the current value of BTC via an API call.
import urllib.request as url
import json
import ast

api_url = ''

currency_input = input("What currency do you want to have? \neur (Euro) \ngbp (Great British Pound) \nusd (United States Dollar) \n:")

def currency_selection(currency_input,api_url):
    base_url = "https://api.coindesk.com/v1/bpi/currentprice/x.json"
    currency_input.upper()
    
    if currency_input == 'EUR':
        print("Selected EUR as the currency")
        api_url = (base_url + currency_input)
        return(api_url)
    
    if currency_input == 'GBP':
        print("Selected GBP as the currency")
        api_url = (base_url + currency_input)
        return(api_url)
    
    if currency_input == 'USD':
        print("Selected USD as the currency")
        api_url = (base_url + currency_input)
        return(api_url)
    
    else:
        print("Invalid Currency!")
        exit()
        
print("The current API URL that will be called is: \n%s" % api_url)

def DataGet(api_url,data):
    request = url.urlopen(url.Request(api_url, headers={'User-Agent' : "Mozilla/5.0"}))
    data = request.read()
    data = data.decode("utf-8")
    data = ast.literal_eval(data)
    return data

print("The current price of BTC in %s (%s) is %s!" % (DataGet(data['bpi'][str(currency_selection(currency_input.upper()))]['code'],DataGet(data['bpi'][str(currency_selection(currency_input.upper()))]['rate'],DataGet(data['bpi'][str(currency_selection(currency_input.upper()))]['description'])))))

    


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.