1
+ # Currency conversion rates relative to USD
2
+ currency_rates = {
3
+ 'USD' : 1.0 , # Base currency
4
+ 'EUR' : 0.85 , # Euro
5
+ 'GBP' : 0.75 , # British Pound
6
+ 'JPY' : 110.0 , # Japanese Yen
7
+ 'AUD' : 1.35 , # Australian Dollar
8
+ 'CAD' : 1.25 , # Canadian Dollar
9
+ 'CHF' : 0.92 , # Swiss Franc
10
+ 'CNY' : 6.45 , # Chinese Yuan
11
+ 'INR' : 73.0 , # Indian Rupee
12
+ 'BRL' : 5.2 , # Brazilian Real
13
+ 'GHC' : 15.71 # Ghana Cedis
14
+ }
15
+
16
+ def convert_currency (amount , from_currency , to_currency , rates ):
17
+ if (from_currency not in rates ) or to_currency not in rates :
18
+ raise ValueError ('Invalid currency' )
19
+
20
+ #Convert amount to USd first
21
+ amount_in_usd = amount / rates [from_currency ]
22
+
23
+ #Convert from usd to target currency
24
+ converted_amount = amount_in_usd * rates [to_currency ]
25
+
26
+ return converted_amount
27
+
28
+ def main ():
29
+ while True :
30
+ print ('Welcome to the Currency Converter! 👌' )
31
+ print ('Available currencies: ' , ',' .join (currency_rates .keys ()))
32
+
33
+ try :
34
+ amount = float (input ('Enter the amount to convert: ' ))
35
+ from_currency = input ('Enter the currency code you are converting from: ' )
36
+ to_currency = input ('Enter the currency code you are converting to: ' )
37
+
38
+ converted_amount = convert_currency (amount , from_currency , to_currency , currency_rates )
39
+ print (f'{ amount } { from_currency } == { converted_amount :.2f} { to_currency } ' )
40
+ except :
41
+ print ('Invalid Input' )
42
+
43
+ options = ('y' , 'n' )
44
+ Continue = input ('Do more conversions (y/n): ' ).lower ()
45
+ if Continue not in options :
46
+ print ('Invalid options' )
47
+
48
+ else :
49
+ if Continue == 'n' :
50
+ print ('Thanks for using the programming...😁' )
51
+ break
52
+ else :
53
+ print ('' )
54
+
55
+
56
+ main ()
0 commit comments