I wrote this in the last hour because I was bored. I was hoping I could get some feedback on this. It is a function that returns the price of a bitcoin and whether you should buy or sell it. I am thinking of setting it up as a function within a chat bot to spice up the chat a little.
import urllib2
import random
import json
def bitcoin(currency, amt = 1):
url = "https://api.coindesk.com/v1/bpi/currentprice.json"
request = urllib2.Request(url)
response = urllib2.urlopen(request)
the_page = response.read()
data = json.loads(the_page)
if currency in ["USD","US","dollar","$"]:
conversion = data['bpi']['USD']['rate_float']
symbol = "USD"
elif currency in ["EUR","EU","euro","€"]:
conversion = data['bpi']['EUR']['rate_float']
symbol = "EUR"
elif currency in ["GBP","GB","UK","pound","£"]:
conversion = data['bpi']['GBP']['rate_float']
symbol = "GBP"
else:
stop("Currency not recognized.")
advice = random.choice(["It's so LOW!!!!!!! \r\nBuy!!! Buy!!! Buy!!!!!","It's so HIGH!!!!!! \r\nSell!!! Sell!!! Sell!!!!!"])
return "{0} Bitcoin = {1} {2} \r\n{3}".format(amt,conversion * amt, symbol, advice)
amt
variable designates the amount of bitcoins to convert. This fact is not clear and I had to look into the string formatting in the last line to understand that. I suggest you either give it a more significant name or add a documentation. Also, what doesstop
do? \$\endgroup\$ – njzk2 Jan 9 '16 at 4:55