Well, of course you can.

You can do what ever you want, and in many ways.
What really helps, is to google your question, if someone out there has done it before.

But I guess, that you are learning now, so let's solve the question at hand.
So, python wont convert "4*3" into math.
That's probably quite good, since it maybe then also would try converting "apples" into math.

So, if you are building a raw_input math calculator, then you could expect symbols like, "*", "/" or so.
So lets make a list with that.

write this is a file called: test.py     Then execute with: python -i test.py    (The -i makes sure you stay in the interpreter)
rawinputstring = raw_input('Enter number: ')

expected_math_symbols = ["*","/"]

for operator in expected_math_symbols:
    if operator in rawinputstring:
        print("Operator found:%s  In string:%s"%(operator,rawinputstring))
        newrawinputstring = rawinputstring.split(operator)
print("I have converted:%s to %s"%(rawinputstring,newrawinputstring))

try:
    answer = float(rawinputstring)
except ValueError:
    if "*" in rawinputstring:
        rawinputstring = rawinputstring.split("*")
        answer = float(rawinputstring[0])*float(rawinputstring[1])
    if "/" in rawinputstring:
        rawinputstring = rawinputstring.split("/")
        answer = float(rawinputstring[0])/float(rawinputstring[1])

print("Your answer you long for is:%s"%(answer))












 
23.08.2011 15:55, Troels Emtekær Linnet yazmış:
Thats because the raw_input is a string.
And python can't find out how to convert the string "4*3" or "4/3" to something usefull.

float("4/3")
float(4*3")

Best
Troels

>>> a = raw_input('Enter number: ')
Enter number: 4*3
>>> type(a)
<type 'str'>
>>> a = raw_input('Enter number: ')
Enter number: 4*3
>>> type(a)
<type 'str'>
>>> a = raw_input('Enter number: ')
Enter number: 4/3
>>> type(a)
<type 'str'>
>>> print a
4/3
>>> a = raw_input('Enter number: ')
Enter number: 4*3
>>> type(a)
<type 'str'>
>>> print a
4*3
>>> a = raw_input('Enter number: ')
Enter number: 4/3
>>> type(a)
<type 'str'>
>>> print a
4/3
>>> float(4*3)
12.0
>>> float("4*3")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 4*3


Troels Emtekær Linnet
Karl-Liebknecht-Straße 53, 2 RE
04107 Leipzig, Tyskland
Mobil:
+49 1577-8944752



2011/8/16 simulacrx <simulacrx@gmail.com>
23.08.2011 15:41, Troels Emtekær Linnet yazmış:
Hmm.

I tried this:

>>> a = raw_input('Enter number: ')
Enter number: 4
>>> a = float(a)
>>> print a
4.0
>>>



Troels Emtekær Linnet
Karl-Liebknecht-Straße 53, 2 RE
04107 Leipzig, Tyskland
Mobil:
+49 1577-8944752



2011/8/16 simulacrx <simulacrx@gmail.com>
23.08.2011 15:34, Troels Emtekær Linnet yazmış:
you could convert it right after input:

a = raw_input('Enter number: ')
a = float(a)

or
a = raw_input('Enter number: ')
b = float(a)

and then use b from that on.

Troels Emtekær Linnet
Karl-Liebknecht-Straße 53, 2 RE
04107 Leipzig, Tyskland
Mobil:
+49 1577-8944752



2011/8/16 simulacrx <simulacrx@gmail.com>
23.08.2011 15:13, Troels Emtekær Linnet yazmış:
>>> ((5+7)/(10-5))*2
4
>>> ((5+7)/float((10-5)))*2
4.7999999999999998
>>> (float((5+7))/(10-5))*2
4.7999999999999998
>>> (float((5+7))/(10-5))**2
5.7599999999999998

the last is to second order.



Troels Emtekær Linnet
Karl-Liebknecht-Straße 53, 2 RE
04107 Leipzig, Tyskland
Mobil:
+49 1577-8944752



2011/8/23 Troels Emtekær Linnet <tlinnet@gmail.com>
Watch the brackets.
They mean "list".

And convert the raw_input into float.


>>> [(5+7)/(10-5)]*2
[2, 2]

>>> ((5+7)/(10-5))*2
4
>>> ((5+7)/float((10-5)))*2
4.7999999999999998
>>>




Troels Emtekær Linnet
Karl-Liebknecht-Straße 53, 2 RE
04107 Leipzig, Tyskland
Mobil:
+49 1577-8944752



2011/8/16 simulacrx <simulacrx@gmail.com>
23.08.2011 13:27, Troels Emtekær Linnet yazmış:
Thats because you do integer division.

Do either:

float(15)/8
15/float(8)
15.0/8
15/8.0

Just be sure, that either 15 or 8 is a float, and not both an integer.

Best
Troels


2011/8/16 simulacrx <simulacrx@gmail.com>
hi;
-----------------------
check=(1,2,3,4,5,6,7,8,9,"/","*","-","+","(",")","[","]")

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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Troels ;

what if i type in raw_input below codes:
(5+8)/(6+4)
there is a kind of calculator which is you can type many math query in same line; like:
----------
>>Type the query:
>>[(5+7)/(10-4)]*2
>>12
---------
in my codes the program works well.. but i cant get division query results with floats. if type that code like:
[(5+7)/(10-5)]*2
the result will be 12 again..




thanx for your  attention about '[ ]'.. i should use ().. i am gonna delete '[ ]' from check set in program.. you wrote :


((5+7)/float((10-5)))*2

we cant expect that user must type 'float' when s/he type some query which includes division.

i have to convert it in source.. but how and where?



unfortunately i tried it..it retuns:
ValueError: invalid literal for float()

then try this and see the result :

>>> a = raw_input('Enter number: ')
Enter number: 4/3
>>> a = float(a)
>>> print eval(a)


so i cant get a float result in eval function on a division query?