[Tutor] Calculator functions part II

Michael Schmitt lgwb@home.com
Wed, 20 Jun 2001 22:17:25 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_00FA_01C0F9D6.C90799D0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Well you might try this as well.

a =3D int(raw_input ("First Number:"))
b =3D int(raw_input ("Second Number:"))
print "Answer: %d" % (a*b)

The problem you have below is that you are trying to multiple the two =
inputs which are still string values.

Just to give you an idea of what I mean change it to:
a =3D int(raw_input ("First Number:"))
b =3D raw_input ("Second Number:")
print "Answer: " , (a*b)

Then you will get the following
First Number:5
Second Number:9
Answer:  99999
=20
As string multiplication is done (5  "9" nines are printed)

Michael Schmitt

  ----- Original Message -----=20
  From: Brendhan Horne=20
  To: tutor@python.org=20
  Sent: Wednesday, June 20, 2001 9:46 PM
  Subject: [Tutor] Calculator functions part II


  I got this response:
  Close, but in most programming languages, the operations go on the =
right
  and the result goes on the left.  That means you want

  ans =3D a * b

  Then all you need to do is print it, like this:

  print 'Answer: ' , ans

  So I now have this:
  #Calculator functions
  a =3D raw_input ("First Number:")
  b =3D raw_input ("Second Number:")
  ans =3D a*b
  print 'Answer:', ans

  I end up with this:
  First Number:8   ( I typed in the 8 )
  Second Number:7 ( Ityped in the 7)
  Traceback (innermost last):
    File "C:\Python20\prog1.py", line 4, in ?
      ans =3D a*b
  TypeError: can't multiply sequence with non-int
  So I am definitly getting warmer just not quite. should I change line =
placement?
  Thanks,
  Brendhan
  -----BEGIN GEEK CODE BLOCK-----
  Version: 3.1
  GE d- s: a C++ U(++) P(++) L+(++) E- W+ N+ o+ K- w+
  O+ M V? PS+ Y PGP(++) t++ 5++ X- R+++ tv++ b+++ DI++
  D G(++) e+(++) h-- r++ y++*
  ------END GEEK CODE BLOCK------


------=_NextPart_000_00FA_01C0F9D6.C90799D0
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 http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4134.600" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>Well you might try this as well.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>a =3D int(raw_input ("First Number:"))<BR>b =3D =
int(raw_input=20
("Second Number:"))<BR>print "Answer: %d" % (a*b)</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>The problem you have below is that you are trying to =
multiple=20
the two inputs which are still string values.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Just to give you an idea of what I mean change it=20
to:</FONT></DIV>
<DIV><FONT size=3D2>a =3D int(raw_input ("First Number:"))<BR>b =3D =
raw_input ("Second=20
Number:")<BR>print "Answer: " , (a*b)</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Then you will get the following</FONT></DIV>
<DIV><FONT size=3D2>First Number:5<BR>Second Number:9<BR>Answer:&nbsp;=20
99999</FONT></DIV>
<DIV><FONT size=3D2>&nbsp;</FONT></DIV>
<DIV><FONT size=3D2>As string multiplication is done (5&nbsp; "9" nines =
are=20
printed)</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV>Michael Schmitt<BR></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A title=3Dbrendhanhorne@hotmail.com=20
  href=3D"mailto:brendhanhorne@hotmail.com">Brendhan Horne</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A title=3Dtutor@python.org =

  href=3D"mailto:tutor@python.org">tutor@python.org</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Wednesday, June 20, 2001 =
9:46=20
  PM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> [Tutor] Calculator =
functions=20
  part II</DIV>
  <DIV><BR></DIV>
  <DIV><FONT size=3D2>I got this response:</FONT></DIV>
  <DIV><FONT size=3D2>Close, but in most programming languages, the =
operations go=20
  on the right<BR>and the result goes on the left.&nbsp; That means you=20
  want<BR><BR>ans =3D a * b<BR><BR>Then all you need to do is print it, =
like=20
  this:<BR><BR>print 'Answer: ' , ans<BR><BR>So I now have =
this:</FONT></DIV>
  <DIV><FONT size=3D2>#Calculator functions<BR>a =3D raw_input ("First=20
  Number:")<BR>b =3D raw_input ("Second Number:")<BR>ans =3D =
a*b<BR>print 'Answer:',=20
  ans<BR></FONT></DIV>
  <DIV><FONT size=3D2>I end up with this:</FONT></DIV>
  <DIV><FONT size=3D2>First Number:8&nbsp;&nbsp; ( I typed in the 8 =
)<BR>Second=20
  Number:7 ( Ityped in the 7)<BR>Traceback (innermost last):<BR>&nbsp; =
File=20
  "C:\Python20\prog1.py", line 4, in ?<BR>&nbsp;&nbsp;&nbsp; ans =3D=20
  a*b<BR>TypeError: can't multiply sequence with non-int</FONT></DIV>
  <DIV><FONT size=3D2>So I am definitly getting warmer just not quite. =
should I=20
  change line placement?</FONT></DIV>
  <DIV><FONT size=3D2>Thanks,<BR>Brendhan<BR>-----BEGIN GEEK CODE=20
  BLOCK-----<BR>Version: 3.1<BR>GE d- s: a C++ U(++) P(++) L+(++) E- W+ =
N+ o+ K-=20
  w+<BR>O+ M V? PS+ Y PGP(++) t++ 5++ X- R+++ tv++ b+++ DI++<BR>D G(++) =
e+(++)=20
  h-- r++ y++*<BR>------END GEEK CODE=20
BLOCK------<BR></DIV></BLOCKQUOTE></FONT></BODY></HTML>

------=_NextPart_000_00FA_01C0F9D6.C90799D0--