Hack Forums
[Python] Currency Converter - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Currency Converter (/showthread.php?tid=4040713)



Currency Converter - XBatt - Feb 4, 2014

Hi guys, I'd like some feedback on my currency conveter, I was told that my previous version was too linear, this is much better in my opinion.

PHP Code

<?php 
#Currency Converter


pound = 1.66
pound_eu 
= 1.21
euro 
= 1.21
euro_d 
= 0.74
euro_y 
= 138.98
dollar 
= 1.66
yen 
= 169.26
yen_d 
= 102.35


def main
():
    print(
"""
[!]Welcome to XBatt's Currency Converter
=======================================
[+]The currenty curreny rates are as following:
[+]The dollar to pound is {0}
[+]The euro to pound is {1}
[+]The yen to pound is {2}
"""
.format(dollar,euro,yen))
    
option = input("""
[+]If you would like to do a currency conversion enter 1
[+]If you would like to change the conversion rates enter 2
[+]If you would like to print the current conversion rates enter 3
"""
)
    if 
option == "1":
        
option1()
    
elif option == "2":
        
option2()
    
elif option == "3":
        
option3()
    else:
        print (
"[+]Option 1,2 or 3 not entered. Try again.\n\n\n\n")
        
main()


def option1():
    
option_convert = input("""
[+]What curreny would you you like to convert and convert to, (Example: dollar & pound) this would allow you to convert dollars to pounds.
"""
)
    
option_convert2 = option_convert.split('&')[0].strip()
    
option_convert3 = option_convert.split('&')[1].strip()
    
    if 
option_convert2 == "dollar" and option_convert3 == "pound":
        
str1 = input ("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num1 = float(str1)
        
result1 = num1 / dollar
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num1,option_convert2,result1,option_convert3))
        
    
elif option_convert2 == "dollar" and option_convert3 == "euro":
        
str2 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num2 = float(str2)
        
result2 = num2 * euro_d
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num2,option_convert2,result2,option_convert3))
        
    
elif option_convert2 == "dollar" and option_convert3 == "yen":
        
str3 = input ("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num3 = float(str3)
        
result3 = num3 * yen_d
        
print ("[+]{0} {1}s is equal to {2} {3}s".format(num3,option_convert2,result3,option_convert3))


        
    
elif option_convert2 == "euro" and option_convert3 == "dollar":
        
str5 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num5 = float(str5)
        
result5 = num5 / euro_d
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num5,option_convert2,result5,option_convert3))
        
    
elif option_convert2 == "euro" and option_convert3 == "yen":
        
str6 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num6 = float(str6)
        
result6 = num6 * euro_y
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num6,option_convert2,result6,option_convert3))
        
    
elif option_convert2 == "euro" and option_convert3 == "pound":
        
str7 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num7 = float(str7)
        
result7 = num7 / pound_eu
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num7,option_convert2,result7,option_convert3))

        
        
    
elif option_convert2 == "yen" and option_convert3 == "pound":
        
str8 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num8 = float(str8)
        
result8 = num8 / pound_eu
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num7,option_convert2,result7,option_convert3))
        
    
elif option_convert2 == "yen" and option_convert3 == "dollar":
        
str9 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num9 = float(str9)
        
result9 = num9 / yen_d
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num9,option_convert2,result9,option_convert3))

    
elif option_convert2 == "yen" and option_convert3 == "euro":
        
str10 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num10 = float(str10)
        
result10 = num10 / euro_y
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num10,option_convert2,result10,option_convert3))



    
elif option_convert2 == "pound" and option_convert3 == "dollar":
        
str11 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num11 = float(str11)
        
result11 = num11 * pound
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num11,option_convert2,result11,option_convert3))

    
elif option_convert2 == "pound" and option_convert3 == "euro":
        
str12 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num12 = float(str12)
        
result12 = num12 * euro
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num12,option_convert2,result12,option_convert3))

    
elif option_convert2 == "pound" and option_convert3 == "yen":
        
str13 = input("[+]How many {0}s would you like to convert to {1}s? ".format(option_convert2,option_convert3))
        
num13 = float(str13)
        
result13 = num13 * yen
        
print("[+]{0} {1}s is equal to {2} {3}s".format(num13,option_convert2,result13,option_convert3))



    else:
        print(
"[+]Something went wrong...")
        
main()
    


def option2():
    print (
"[+]Welcome to the currency rates changer section, to change the rates please answer the following questions.")
    
c1 = input("How many dollars in each pound? ")
    
c1num = float(c1)
    
pound = c1

    c2 
= input(" How many yen in a euro? ")
    
c2num = float(c2)
    
euro_y

    c3 
= input("How many euros in a pound?")
    
c3num = float(c3)
    
euro = c3

    c4 
= input("How many euros in a dollar?")
    
c4num = float(c4)
    
euro_d = c4

    c4 
= input("How many dollars in a pound?")
    
c4num = float(c4)
    
dollar = c4

    c5 
= input("How many pounds in a yen?")
    
c5num = float(c5)
    
yen = c5

    c6 
= input("How many yens in a dollar?")
    
c6num = float(c6)
    
yen_d = c6

    
print("Congratulations, you have changed the curent conversion rates, the program will now restart so you can convert!")
    
main()


def option3():

    
text_file = open("Current_Exchange_Rates.txt", "w")

    
text_file.write("£1 = £{0} \n".format(dollar))
    
text_file.write("£1 = €{0} \n".format(euro))
    
text_file.write("£1 = ¥{0} \n".format(yen_d))

    
text_file.close()
    print(
"The text file has been created in the directory of this script!")

if 
__name__ == "__main__":
    
main()
    
end = input("Enter q to exit or c to restart the progarm")
    if 
end == "q":
        print(
"Bye!")
    
elif end == "c":
        
main() 



* - Tony S - Feb 4, 2014

The president is murdered. The entire of the United States collapses. The price of a Dollar falls to minus 15 dollars.

Your program is now invalid.

shoulda used an API


* - XBatt - Feb 4, 2014

(Feb 4, 2014 12:42 PM)Tony Spilotro Wrote:  The president is murdered. The entire of the United States collapses. The price of a Dollar falls to minus 15 dollars.

Your program is now invalid.

shoulda used an API
In the task I was given, It had to be manual conversions.


* - Tony S - Feb 4, 2014

(Feb 4, 2014 12:43 PM)XBatt Wrote:  In the task I was given, It had to be manual conversions.

It could still be "manual" (?) if you used an API.


* - XBatt - Feb 4, 2014

(Feb 4, 2014 12:44 PM)Tony Spilotro Wrote:  It could still be "manual" (?) if you used an API.
By manual I mean like do the maths involved. And what part does the dollar fall to minus 15..?


* - Tony S - Feb 4, 2014

(Feb 4, 2014 12:45 PM)XBatt Wrote:  By manual I mean like do the maths involved. And what part does the dollar fall to minus 15..?

It was a joke. Ha, ha.

You can use an API (that provides an accurate pricing of currencies) to grab the value of each currency and still do whatever maths you want.


* - XBatt - Feb 4, 2014

(Feb 4, 2014 12:46 PM)Tony Spilotro Wrote:  It was a joke. Ha, ha.

You can use an API (that provides an accurate pricing of currencies) to grab the value of each currency and still do whatever maths you want.
Yeah, I understand what you mean but I don't want to use an API. Apart from that... whats your opinion on the actual code? :P


* - Tony S - Feb 4, 2014

(Feb 4, 2014 12:49 PM)XBatt Wrote:  Yeah, I understand what you mean but I don't want to use an API. Apart from that... whats your opinion on the actual code? :P

There's a lot of unnecessary code.

If I were going to do it like you are, I would make a dictionary of every currency I want to include. Take one currency and set the value of every other currency relative to that. For example, pick the dollar. Make the value of yen relative to the price of one dollar.

So, to find how much one currency is worth compared to another, first find the price of it in dollars, and then convert that to the intended currency.

I'd probably do something like -

Code
currencies = {
    'usd': 1.00,
    'gbp': 0.61,
    'yen': 101.63
}

def currency_to_dollar(currency, amount):
    return (currencies['usd'] / currencies[currency]) * amount

def dollar_to_currency(currency, amount):
    return round(currencies[currency] * amount)

def currency_to_currency(old, new):
    return round(dollar_to_currency(new, currency_to_dollar(old[0], old[1])), 2)

print currency_to_currency(
    ['gbp', 1.66],
    'yen'
)