-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.py
More file actions
35 lines (29 loc) · 1.01 KB
/
logic.py
File metadata and controls
35 lines (29 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import requests
class CalculatorLogic:
def __init__(self):
self.expression = ""
def clear(self):
self.expression = ""
def append(self, value):
self.expression += str(value)
def evaluate(self):
try:
return str(eval(self.expression))
except Exception:
return "Error"
def convert_currency(self, amount, from_currency, to_currency):
try:
url = f"https://api.exchangerate.host/convert?from={from_currency}&to={to_currency}&amount={amount}"
response = requests.get(url)
data = response.json()
return str(data.get("result", "Error"))
except:
return "Error"
def get_crypto_price(self, symbol):
try:
url = f"https://api.coingecko.com/api/v3/simple/price?ids={symbol}&vs_currencies=usd"
response = requests.get(url)
data = response.json()
return str(data[symbol]["usd"])
except:
return "Error"