Skip to content

Commit fba26d7

Browse files
committed
hw5
1 parent 48dc4fa commit fba26d7

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Diff for: homework5.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
4+
print("Hello, please choose select a country by number:")
5+
6+
result = requests.get("https://www.iban.com/currency-codes")
7+
soup = BeautifulSoup(result.text, "html.parser")
8+
tr = soup.find_all('tr')
9+
tds = soup.find_all('td')
10+
11+
data = []
12+
13+
for tr in soup.find_all('tr'):
14+
tds = list(tr.find_all('td'))
15+
if tr.find_all('td'):
16+
country = tds[0].text
17+
currency = tds[1].text
18+
code = tds[2].text
19+
number = tds[3].text
20+
data.append([country,currency,code,number])
21+
22+
for i in range(len(data)):
23+
print(f"# {i+1}", data[i][0])
24+
i+=1
25+
26+
def answer():
27+
try:
28+
answer_1 = int(input("Where are you from? Choose a country by number.\n #:"))
29+
if 0 < answer_1 < 269:
30+
print(data[answer_1-1][0])
31+
elif answer_1 <1 or answer_1>268:
32+
print("Choose from a number list.")
33+
answer()
34+
except:
35+
print("This is not a number.")
36+
answer()
37+
38+
return answer_1
39+
40+
def answer2():
41+
try:
42+
answer_2 = int(input("\nNow choose another country. \n"))
43+
if 0 < answer_2 < 269:
44+
print(data[answer_2-1][0])
45+
elif answer_2 < 1 or answer_2 > 268:
46+
print("Choose from a number list.")
47+
answer2()
48+
except:
49+
print("This is not a number.")
50+
answer2()
51+
52+
return answer_2
53+
54+
def convert():
55+
from_country = answer()
56+
to_country = answer2()
57+
58+
answer_3 = int(input(f"\nHow many {data[from_country-1][1]} do you want to convert to {data[to_country-1][1]}?\n"))
59+
60+
tw = requests.get(f"https://wise.com/gb/currency-converter/{data[from_country][2]}-to-{data[to_country-1][2]}-rate?amount={answer_3}")
61+
tw_soup = BeautifulSoup(tw.text, "html.parser")
62+
converting = float(tw_soup.find('span',{"class":"text-success"}).text)
63+
result = float(converting*answer_3)
64+
print(f"{data[from_country-1][1]} {answer_3} is {data[to_country-1][1]} {result}")
65+
return result
66+
67+
convert()

0 commit comments

Comments
 (0)