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=3857188)



Currency Converter - XBatt - Nov 10, 2013

Here is a currency converter I made, I know it's not very efficient and there are other ways to make this better. Please provide suggestions, also what does the "def" do in python?

PHP Code

<?php 
#CURRENT EXCHANGE RATES
pound = 1.00
euro 
= 1.19
dollar 
= 1.60 
yen 
= 158.33
#CURRENT EXCHANGE RATES

print ("Welcome to my Currency Converter")
print (
"==")
print (
"The current exchange rates are:")
print (
"\n£1 = $1.60")
print (
"\n£1 = €1.19")
print (
"\n£1 = ¥158.33")
ccinput = input("If you would like to change the currency please enter 1\nIf you would like to do a currency conversion enter 2\nIf you would like to print a text file with the current exchange rates enter 3\nYour selection:")
if 
ccinput == ("1"):
    print (
"You have choosen too change the exchange rates")
    print (
"This is currently in progress")

elif ccinput == ("2"):
  print (
"\n==")
  print (
"You have choosen to convert a currency")
  print (
"==")
  
currencychoice = input("Please choose which currency to exchange, pound, euro, dollar or yen?\nYour selection:")

  
##
  
if currencychoice == ("pound"):
    
poundselect1 = input ("What currency would you like to exchange your pound to?:")

    if 
poundselect1 == ("euro"):
    
poundeuroselect = input("You have choosen to exchange pound to euro, how many pounds would you like to exchange to euro?")
    
intconvert1 = int(poundeuroselect)
    print (
intconvert1*euro)

    
elif poundselect1 == ("dollar"):
    
pounddollarselect = input("You have choosen to exchange pound to dollar, how many pounds would you like to exchange to dollar?")
    
intconvert2 = int(pounddollarselect)
    print (
intconvert2*dollar)

    
elif poundselect1 == ("yen"):
    
poundyenselect = input("You have choosen to exchange pound to yen, how many pounds would you like to exchange to yen?")
    
intconvert3 = int(poundyenselect)
    print (
intconvert3*yen)
    else:
    print (
"FAIL, please try again.")    

  else:
    
1 + 1

  
##

    
if currencychoice == ("euro"):
    
euroselect1 = input ("What currency would you like to exchange your euro to?:")

    if 
euroselect1 == ("pound"):
    
europoundselect = input("You have choosen to exchange euro to pound, how many pounds would you like to exchange to euro?")
    
intconvert4 = int(europoundselect)
    print (
intconvert4*0.84)

    
elif euroselect1 == ("dollar"):
    
eurodollarselect = input("You have choosen to exchange pound to dollar, how many pounds would you like to exchange to dollar?")
    
intconvert5 = int(eurodollarselect)
    print (
intconvert2*dollar)

    
elif euroselect1 == ("yen"):
    
euroyenselect = input("You have choosen to exchange pound to yen, how many pounds would you like to exchange to yen?")
    
intconvert6 = int(euroyenselect)
    print (
intconvert6*yen)
    else:
  print (
"FAIL, please try again.")

    else:
    
1 + 1

  
##

    
if currencychoice == ("dollar"):
    
dollarselect1 = input ("What currency would you like to exchange your dollar to?:")

    if 
dollarselect1 == ("pound"):
    
dollarpoundselect = input("You have choosen to exchange euro to pound, how many pounds would you like to exchange to euro?")
    
intconvert7 = int(dollarpoundselect)
    print (
intconvert7*0.62)

    
elif dollarselect1 == ("euro"):
    
dollareuroselect = input("You have choosen to exchange pound to dollar, how many pounds would you like to exchange to dollar?")
    
intconvert8 = int(dollareuroselect)
    print (
intconvert8*euro)

    
elif dollarselect1 == ("yen"):
    
dollaryenselect = input("You have choosen to exchange pound to yen, how many pounds would you like to exchange to yen?")
    
intconvert9 = int(dollaryenselect)
    print (
intconvert9*yen)
    else:
  print (
"FAIL, please try again.")  
    else:
    
1 + 1

elif ccinput 
== ("3"):
    
text_file = open("current_exchange_rates.txt", "w")

    
text_file.write("£1 = $1.60 \n")
    
text_file.write("£1 = €1.19 \n")
    
text_file.write("£1 = ¥158.33\n")

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

else:
    print (
"Sorry you have crashed the program please restart :P")

input("...Please press enter to close:...") 

Thanks


* - glob - Nov 10, 2013

Wow. Yes that code could be improved, a lot. def means declare function. Simply put, it basically creates a variable that, when called, will execute instructions.

PHP Code

<?php 
def hi
():  #Creates a function under the name "hi"
  
print "Hello World"  #This is where the body of the functin is (the instructions executed)
hi()  #This is the function call, and when this is called, all the instructions within the function body will be executed successively 

The body of the function is not executed until the function is called.

You can read more about functions here:
http://docs.python.org/release/1.5.1p1/tut/functions.html
and here:
http://www.tutorialspoint.com/python/python_functions.htm


* - XBatt - Nov 10, 2013

Ah, that's quite easy to understand. And how can I improve the code?

Also, when writing out the body of the def how do you tell python this is the end of the body of def?


* - glob - Nov 10, 2013

(Nov 10, 2013 11:30 AM)XBatt Wrote:  Ah, that's quite easy to understand. And how can I improve the code?

Also, when writing out the body of the def how do you tell python this is the end of the body of def?

The body of the function is indented. Once indentation ceases, the interpreter assumes that that is the end of the function declaration.


* - XBatt - Nov 10, 2013

(Nov 10, 2013 12:26 PM)Cᴀᴛᴀʟʏsᴛ Wrote:  The body of the function is indented. Once indentation ceases, the interpreter assumes that that is the end of the function declaration.

Ah okay, and how can I make my currency converter more efficient? or simpler for the user?


Can anyone help me with this? Advice? Tips?