[Tutor] eval func with floating...
Chanakya Mitra
chanakya at optirisk-systems.com
Tue Aug 23 16:51:43 CEST 2011
c=set(a).intersection(set(check))
shouldn’t this be:
c=set(a).issubset(set(check))
?
set(a).intersection(set(check)) will be true as long as only one element appears in both. ie. 15/a8 kill the process and spit out an error instead of asking "error! please use -defined operators-!"
Sent: 23 August 2011 12:53
To: tutor at python.org
Subject: Re: [Tutor] eval func with floating...
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/e8dad0a5/attachment-0001.html>
More information about the Tutor
mailing list