1
+ import os
1
2
import requests
2
3
from bs4 import BeautifulSoup
3
4
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
- answer ()
5
+ os .system ("clear" )
6
+
7
+
8
+ url = "https://www.iban.com/currency-codes"
9
+
10
+
11
+ countries = []
12
+
13
+ request = requests .get (url )
14
+ soup = BeautifulSoup (request .text , "html.parser" )
15
+
16
+ table = soup .find ("table" )
17
+ rows = table .find_all ("tr" )[1 :]
18
+
19
+ for row in rows :
20
+ items = row .find_all ("td" )
21
+ name = items [0 ].text
22
+ code = items [2 ].text
23
+ if name and code :
24
+ country = {
25
+ 'name' :name .capitalize (),
26
+ 'code' : code
27
+ }
28
+ countries .append (country )
29
+
30
+
31
+ def ask ():
32
+ try :
33
+ choice = int (input ("#: " ))
34
+ if choice >= len (countries ) or choice < 0 :
35
+ print ("Choose a number from the list." )
36
+ ask ()
37
+ else :
38
+ country = countries [choice ]
39
+ print (f"You chose { country ['name' ]} \n The currency code is { country ['code' ]} " )
40
+ except ValueError :
41
+ print ("That wasn't a number." )
42
+ ask ()
43
+
44
+
45
+ print ("Hello! Please choose select a country by number:" )
46
+ for index , country in enumerate (countries ):
47
+ print (f"#{ index } { country ['name' ]} " )
48
+
49
+ ask ()
0 commit comments