[Tutor] Numbers appearing as strings in program
Albert Antiquera
gundamone@hotmail.com
Sun, 13 Aug 2000 11:07:47 -0400
This is a multi-part message in MIME format.
------=_NextPart_000_0011_01C00516.B66278A0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi to all,
I've written this simple calculator program but I've got a problem with =
it: The NUMBERS are "turning" to STRINGS!! I'm not sure what is wrong =
cuz I'm new to Python. Does the built in statement 'raw_input' only =
support strings???If so, how can I get numbers as input from the user? =
Here is the script calculator.py:
#---------------------------------Begin =
Script---------------------------------------------
#!/usr/local/bin/python
# Author: Albert P. Antiquera, 2000
# Program : Simple calculator
import sys
def add(x,y):
sum =3D x + y
print "The sum of", x , "and" , y ,"is:", sum
=20
def sub(x,y):
dif =3D x - y
print "The difference of", x , "minus", y , "is:", dif
=20
def mul(x,y):
pro =3D x * y
print "The product of", x , "times", y , "is:", pro
=20
def div(x,y):
quo =3D x / y
print "The quotient of", x , "divided by", y , "is:", quo
=20
print "Press A for Addition"
print " S for Subtraction"
print " M for Multiplication"
print " D for Division"
choice =3D raw_input("Enter what kind of operation you want:")
if (choice =3D=3D 'A'):
x =3D raw_input("Enter a value for X:")
y =3D raw_input("Enter a value for Y:")
add(x,y)
=20
elif (choice =3D=3D 'S'):
x =3D raw_input("Enter a value for X:")
y =3D raw_input("Enter a value for Y:")
sub(x,y)
elif (choice =3D=3D 'M'):
x =3D raw_input("Enter a value for X:")
y =3D raw_input("Enter a value for Y:")
mul(x,y)
elif (choice =3D=3D 'D'):
x =3D raw_input("Enter a value for X:")
y =3D raw_input("Enter a value for Y:")
div(x,y)
else:
print "Wrong input.Make sure key is in UPPER CASE"
=20
#------------------------------End of =
Script--------------------------------
Another thing, the Addition function works but instead of adding the =
numbers like (1 + 1 =3D 2) it concatenates it like (1 + 1 =3D 11). The =
rest of the functions don't work at all. Any help would be deeply =
appreciated.
Thanks,
Albert
URL: www.isopod-graphics.com
email: albert@isopod-graphics.com
------=_NextPart_000_0011_01C00516.B66278A0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi to all,</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>I've written this simple calculator =
program but=20
I've got a problem with it: The NUMBERS are "turning" to STRINGS!! I'm =
not sure=20
what is wrong cuz I'm new to Python. Does the built in statement =
'raw_input'=20
only support strings???If so, how can I get numbers as input from the =
user? Here=20
is the script calculator.py:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>#---------------------------------Begin =
Script---------------------------------------------</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>#!/usr/local/bin/python<BR># Author: =
Albert P.=20
Antiquera, 2000<BR># Program : Simple calculator<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>import sys</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>def add(x,y):<BR> sum =3D x + =
y<BR> print=20
"The sum of", x , "and" , y ,"is:", sum<BR> <BR>def =
sub(x,y):<BR> dif=20
=3D x - y<BR> print "The difference of", x , "minus", y , "is:",=20
dif<BR> <BR>def mul(x,y):<BR> pro =3D x * y<BR> print =
"The product=20
of", x , "times", y , "is:", pro<BR> <BR>def div(x,y):<BR> quo =
=3D x /=20
y<BR> print "The quotient of", x , "divided by", y , "is:",=20
quo<BR> <BR>print "Press A for Addition"<BR>print=20
" S for Subtraction"<BR>print=20
" M for Multiplication"<BR>print=20
" D for Division"<BR>choice =3D =
raw_input("Enter=20
what kind of operation you want:")</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>if (choice =3D=3D 'A'):<BR> x =3D =
raw_input("Enter a=20
value for X:")<BR> y =3D raw_input("Enter a value for=20
Y:")<BR> add(x,y)<BR> <BR>elif (choice =3D=3D 'S'):<BR> x =
=3D=20
raw_input("Enter a value for X:")<BR> y =3D raw_input("Enter a =
value for=20
Y:")<BR> sub(x,y)<BR>elif (choice =3D=3D 'M'):<BR> x =3D =
raw_input("Enter a=20
value for X:")<BR> y =3D raw_input("Enter a value for=20
Y:")<BR> mul(x,y)<BR>elif (choice =3D=3D 'D'):<BR> x =3D =
raw_input("Enter a=20
value for X:")<BR> y =3D raw_input("Enter a value for=20
Y:")<BR> div(x,y)<BR>else:<BR> print "Wrong input.Make sure =
key is in=20
UPPER CASE"<BR> </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>#------------------------------End of=20
Script--------------------------------</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Another thing, the Addition function =
works but=20
instead of adding the numbers like (1 + 1 =3D 2) it concatenates it like =
(1 + 1 =3D=20
11). The rest of the functions don't work at all. Any help would be =
deeply=20
appreciated.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Thanks,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Albert</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>URL: <A=20
href=3D"http://www.isopod-graphics.com">www.isopod-graphics.com</A><BR>em=
ail: <A=20
href=3D"mailto:albert@isopod-graphics.com">albert@isopod-graphics.com</A>=
</FONT></DIV></BODY></HTML>
------=_NextPart_000_0011_01C00516.B66278A0--