Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Currency Converter

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
Things I love - Pentesting,Python,Linux,CryptoCurrencies,Legion,Server Administration,Raspberry Pi - PM me if you wanna talk!
Freelance Pentester and Python coder.
[Image: 2EOBWgX.png]
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/t...tions.html
and here:
http://www.tutorialspoint.com/python/pyt...ctions.htm
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?
Things I love - Pentesting,Python,Linux,CryptoCurrencies,Legion,Server Administration,Raspberry Pi - PM me if you wanna talk!
Freelance Pentester and Python coder.
[Image: 2EOBWgX.png]
(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.
(Nov 10, 2013 12:26 PM)glob 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?
Things I love - Pentesting,Python,Linux,CryptoCurrencies,Legion,Server Administration,Raspberry Pi - PM me if you wanna talk!
Freelance Pentester and Python coder.
[Image: 2EOBWgX.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by MymannNov 8, 2017 12:11 PM 5 301 Nov 8, 2017 12:11 PM
Last Post: ahemokay
[.NET] by N1nj4Styl3Sep 5, 2015 12:50 AM 9 616 Sep 5, 2015 12:50 AM
Last Post: Odus
[Python] by XBattFeb 4, 2014 01:07 PM 7 167 Feb 4, 2014 01:07 PM
Last Post: Tony S