[Tutor] Calculator functions part II

ak@silmarill.org ak@silmarill.org
Wed, 20 Jun 2001 23:22:04 -0400


On Wed, Jun 20, 2001 at 10:46:21PM -0400, Brendhan Horne wrote:
> 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 = a * b
> 
> Then all you need to do is print it, like this:
> 
> print 'Answer: ' , ans
> 
> So I now have this:
> #Calculator functions
> a = raw_input ("First Number:")
> b = raw_input ("Second Number:")

Now you got 2 strings, a and b. You want them to be integers, however.

> ans = a*b

ans = int(a)*int(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 = a*b
> TypeError: can't multiply sequence with non-int

string is a sequence type. you can multiply it by an integer, i.e. 
'ab'*3 is ababab, but '3'*'7' is meaningless. '7'*3 is 777 and what you really
need here is 3*7 (or whatever).

> 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------
> 

-- 
Jupiter and Saturn Oberon Miranda
And Titania Neptune Titan
Stars can frighten
        - Syd