[Tutor] eval func with floating...

Joel Goldstick joel.goldstick at gmail.com
Tue Aug 23 13:53:05 CEST 2011


On Tue, Aug 23, 2011 at 6:52 AM, Peter Otten <__peter__ at web.de> wrote:

> simulacrx wrote:
>
> > check=(1,2,3,4,5,6,7,8,9,"/","*","-","+","(",")","[","]")
>
> You have to quote the digits: 1 is an integer while "1" is a string of
> length one:
>
> >>> "1" == 1
> False
>
> Also, you forgot the "0".
> Note that there's no need to use a tuple as set() will happily accept a
> string:
>
> check = set("0123456789/*-+,()")
>
> > while True:
> >      a=raw_input("type your query : \n")
> >      c=set(a).intersection(set(check))
> >
> >      if c:
> >          print *eval("float(%s)"%a)*
> >      else:
> >          print "error! please use -defined operators-!"
> > -----------------------------
> >
> > this script works.. but if i type
> >
> > 15/8
> >
> > it returns 1.. no floatin'.. i must fix this? can anyone help me?
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

I'm not sure if the performance difference would matter, but if you make
check a set above your loop there is no need to use the set(check) in the
loop when check will do

-- 
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110823/fa06a0be/attachment.html>


More information about the Tutor mailing list