Re: [Tutor] Can I get some feedback on my currency converter program

2005-12-09 Thread Alan Gauld
> like in Java, i would like to have live currency quotes fed to my 
> function,

Look at the urllib module.
However I suspect you might be better working on the basics for a little
longer before getting too ambitious!

def rupees_from_dollars(d_doll):
return 43*(d_doll)

You don;t need the () in the line above.
They are needed as part of the function definition but not
within the function unless you need to group things together.
Here you only have one value so there is no need for the ()


choice = "p"
while choice != "q":
if choice == "$":
Rupees = input("US Dollars:")
print "Rupees:",rupees_from_dollars(Rupees)

It might be better toi choose a more apt name for the input
value - dollars in this case maybe? Then it would look like:

if choice == "$":
dollars = input("US Dollars:")
print "Rupees:",rupees_from_dollars(dollars)

It just makes a bit more sense since the value being read is,
in fact, the dollar value not the Rupee value.

choice = raw_input("Choose option:")

You could also put the raw_input statement inside your menu function
and have it return the chosebn value - just a thought.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can I get some feedback on my currency converter program

2005-12-09 Thread Danny Yoo


> On Fri, 9 Dec 2005, vikas mohan wrote:
>
> > Thanks! Looks in a better shape now! I have a diffrent question now:
> > What if like in Java, i would like to have live currency quotes fed to
> > my function, from some forex dealer website? So, that my program can
> > provide uptodate info. How can I hot/live link my function?


Hi Vikas,

Ok, so I looked into this a bit more; I think you mean to grab the
currency rates that are being used at any given time.  There's a web site
on finance.yahoo.com that gives quotes:

http://finance.yahoo.com/currency

and it seems common for people to write tools to scrap information off
that page.  We can grab such pages in Python by using the 'urllib'
library.

http://www.python.org/doc/lib/module-urllib.html


For example, the following program:

##
import urllib
text = urllib.urlopen("http://finance.yahoo.com/d/quotes.csv"; +
  "?s=USDJPY=X&f=sl1d1t1ba&e=.csv").read()
print text
##

shows how we can download a page and get some data --- the program above
grabs data for the US-to-Japanese-Yen exchange rate.  If you take this
route, you'll probably need to do a bit more to make this work across all
the currency types you want.

If you look around the web, you should be able to find some module that
people have written to do this.



Besides Yahoo, there appears to be other places where live
currency-exchange data can be grabbed in bulk.  For example, the European
Central Bank (ECB) appears to provide this data against the Euro:

http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html

Not only does ECB provides this information in HTML, but also in a
machine-parsable XML format:

http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

So one way to grab the live data would be to take the above XML document
and use an XML parser to yank out the data values.  One popular one that
we've been using recently is a third-party module called ElementTree,
which can be found here:

http://effbot.org/zone/element-index.htm

This seems to be XML-parsing week for me, so for fun, I wrote something to
parse the content out.  If you don't want to be spoiled, don't read the
end of this message.  *grin*


** spoiler space ahead **





** spoiler space **

Here's a small program that can do this retrieval and parsing.  (I'm still
an ElementTree newbie, so there may be a more concise way of programming
the following):

##
"""A small module to grab the reference rates against the Euro, using
statistics from the European Central Bank."""

import urllib
from elementtree import ElementTree

def getLiveEuroData():
"""Returns a dictionary mapping currency codes from the Euro to
that particular currency."""
tree = getTree()
euroData = {}
for elt in tree.findall(".//*"):
if (elt.attrib.get('currency', None) and
elt.attrib.get('rate', None)):
euroData[elt.attrib.get('currency')] = (
float(elt.attrib.get('rate')))
return euroData

def getTree():
"""Grabs the latest xml document from the European Central Bank at
http://www.ecb.int.""";
f = urllib.urlopen("http://www.ecb.int/stats/eurofxref/"; +
   "eurofxref-daily.xml")
tree = ElementTree.parse(f)
f.close()
return tree
###


So, for example, to find the currency exchange rate from the Euro to US
dollars, we can do:

#
>>> euroToOtherCurrencyRate = getLiveEuroData()
>>> euroToOtherCurrencyRate['USD']
1.17850001
#


If we wanted to be nice, we'd probably add something to cache the exchange
rates somewhere in memory, so that we don't hammer ECB.



Anyway, I hope this comes in handy!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can I get some feedback on my currency converter program

2005-12-09 Thread Danny Yoo


On Fri, 9 Dec 2005, vikas mohan wrote:

> Thanks! Looks in a better shape now! I have a diffrent question now:
> What if like in Java, i would like to have live currency quotes fed to
> my function, from some forex dealer website? So, that my program can
> provide uptodate info. How can I hot/live link my function?

Hi Vikas,

I'm not familiar with the terminology of 'hot/live linking', and my Google
searches on this aren't effective.  ('hot live linking java' is giving me
spicy coffee recipes.  *grin*)

Can you point out how you'd do what you want in Java?  That'll help us
understand the question better.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can I get some feedback on my currency converter program

2005-12-09 Thread vikas mohan
Thanks! Looks in a better shape now! I have a diffrent question now: What if like in Java, i would like to have live currency quotes fed to my function, from some forex dealer website? So, that my program can provide uptodate info. How can I hot/live link my function?

 
/VikasM
 
#converts currencies indo Indian rupeesprint "Welcome to your currency converter\n"print "Plese choose from the following opitons\n"def print_options():
    print "Options:"    print " 'p' print options"    print " '$' convert from US Dollars to Indian Rupees"    print " 'kr' convert from Swedish Kronors to Indian Rupees"
    print " '£' convert from British Pound Sterling to Indian Rupees"    print " 'eu' convert from Euros to Indian Rupees"    print " 'q' quit the program"def rupees_from_dollars(d_doll):
    return 43*(d_doll)def rupees_from_kronor(kr_kron):    return 8*(kr_kron)def rupees_from_pounds(p_pound):    return 68*(p_pound)def rupees_from_euros(eu_euros):    return 54*(eu_euros)
choice = "p"while choice != "q":    if choice == "$":    Rupees = input("US Dollars:")    print "Rupees:",rupees_from_dollars(Rupees)    elif choice == "kr":
    Rupees = input("Swedish Kronor:")    print "Rupees:",rupees_from_kronor(Rupees)    elif choice == "£":    Rupees = input("UK Sterling:")    print "Rupees:",rupees_from_pounds(Rupees)
    elif choice == "eu":    Rupees = input("Euros:")    print "Rupees:",rupees_from_euros(Rupees)    elif choice != "q":    print_options()   
    choice = raw_input("Choose option:")
 
On 12/9/05, Ewald Ertl <[EMAIL PROTECTED]> wrote:
Hi!vikas mohan wrote:> Hi all, this is my first program in python.>> It's a currency converter mini-program, but I am not getting the desired
> output. What wrong am I doing here?>> *Program code:*>> #converts currencies to Indian rupees>> def print_options():> print "Options:"> print " 'p' print options"
> print " 'c' convert from US Dollars to Indian Rupees"> print " 'f' convert from Swedish Kronors to Indian Rupees"> print " 'g' convert from British Pound Sterling to Indian Rupees"
> print " 'q' quit the program">> def rupees_from_dollars(c_doll):> return 43*1 --*The function is supposed to multiply the value ofHere is the c_doll missing   return 43*c_doll
> dollars x 43(value of an Indian rupee)> *def rupees_from_kronor(f_kron):> return 8*f_kron -- *The function is supposed to multiply the value> of kronor x 8(value of an Indian rupee)*
> def rupees_from_pounds(g_pound):> return 68*g_pound --*The function is supposed to multiply the value> of pounds x 68(value of an Indian rupee) *>> choice = "p"> while choice != "q":
> if choice == "c":> Rupees = input("US Dollars:")Rupees contains the inserted value> print "Rupees:",--*The problem is here: I only get Rupees but
Here just the Text"Rupees:" is printed print "Rupees:", rupees_from_dollars(Rupees)orprint "Rupees: %d\n" % ( rupees_from_dollars( Rupees ) )You have to call the function to get the converted value.
> not the value multiplied as stated in the function. *> elif choice == "f":> Rupees = input("Swedish Kronor:")> print "Rupees:" --*The problem is here: I only get Rupees but
> not the value multiplied as stated in the function. *> elif choice == "g":> Rupees = input("UK Sterling:")> print "Rupees:"--*The problem is here: I only get Rupees but not
> the value multiplied as stated in the function. *> elif choice != "q":> print_options()> choice = raw_input("option:")>> -
>> Much appreciate your help!>> VikasM>> **> **>>> >> ___
> Tutor maillist  -  Tutor@python.org> http://mail.python.org/mailman/listinfo/tutorHTH Ewald

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can I get some feedback on my currency converter program

2005-12-09 Thread Ewald Ertl
Hi!

vikas mohan wrote:
> Hi all, this is my first program in python.
>  
> It's a currency converter mini-program, but I am not getting the desired
> output. What wrong am I doing here?
>  
> *Program code:*
>  
> #converts currencies to Indian rupees
> 
> def print_options():
> print "Options:"
> print " 'p' print options"
> print " 'c' convert from US Dollars to Indian Rupees"
> print " 'f' convert from Swedish Kronors to Indian Rupees"
> print " 'g' convert from British Pound Sterling to Indian Rupees"
> print " 'q' quit the program"
> 
> def rupees_from_dollars(c_doll):
> return 43*1 --*The function is supposed to multiply the value of

Here is the c_doll missing
return 43*c_doll

> dollars x 43(value of an Indian rupee)
> *def rupees_from_kronor(f_kron):
> return 8*f_kron -- *The function is supposed to multiply the value
> of kronor x 8(value of an Indian rupee)*
> def rupees_from_pounds(g_pound):
> return 68*g_pound --*The function is supposed to multiply the value
> of pounds x 68(value of an Indian rupee) *
> 
> choice = "p"
> while choice != "q":
> if choice == "c":
> Rupees = input("US Dollars:")
Rupees contains the inserted value
> print "Rupees:",--*The problem is here: I only get Rupees but
Here just the Text"Rupees:" is printed

  print "Rupees:", rupees_from_dollars(Rupees)

orprint "Rupees: %d\n" % ( rupees_from_dollars( Rupees ) )

You have to call the function to get the converted value.

> not the value multiplied as stated in the function. *
> elif choice == "f":
> Rupees = input("Swedish Kronor:")
> print "Rupees:" --*The problem is here: I only get Rupees but
> not the value multiplied as stated in the function. *
> elif choice == "g":
> Rupees = input("UK Sterling:")
> print "Rupees:"--*The problem is here: I only get Rupees but not
> the value multiplied as stated in the function. *
> elif choice != "q":
> print_options()
> choice = raw_input("option:")
> 
> -
> 
> Much appreciate your help!
> 
> VikasM
> 
> ** 
> ** 
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

HTH Ewald

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor