[Tutor] RE: I don't understand why it doesn't work :(

Guillaume willblake@wanadoo.fr
Tue Jun 3 13:00:02 2003


Hi,
I expected 17 because I tought that
print int (s) convert my string in an integer
so that the addition between 3*4 and 5 was possible.
As far as the remainder, I'm searching actively, promise
I'll give it as soon as I have it clearly in my mind :)
Amitiés à toi
PS:tu peux me dire "tu". Moi je dis "tu" à tous mes amis ;)

-----Message d'origine-----
De : Charlie Clark [mailto:charlie@begeistert.org]
Envoyé : mardi 3 juin 2003 16:58
À : tutor@python.org; willblake@wanadoo.fr
Objet : Re: I don't understand why it doesn't work :(


Bonjour Guillaume et bienvenue sur Python-Tutor. Est-ce que possible que
vous n'envoyez pas les messages en HTML?


> Subject: [Tutor] The remainder %
>
> C'est un message de format MIME en plusieurs parties.
>
> Hello,
> I'm not very good in maths and I don't understand why
> 14 % 3 = 2
> Could someone explain me this mystery?
> Thanks in advance :)

Quite easy really. The remainder is that whole number which remains when
you divide one number by another. 14 isn't entirely divisible by 3 so there
will be a remainder of 1 or 2. 4 x 3 is 12 and 14 - 12 is 2.

What is the remainder of 21 / 7 ?

> Hi,
> My problem is quite simple.
> Here is the prog:
>  s = "3 * 4"
> print type (s)
> <type 'str'>
>
> Easy but then I tried:
> print int (s) + 5
> I was waiting for something like 17
> but it doesn't work :(
> Why?
> This is my only question.

Why were you expecting 17? You added a number to a string.

Try this:
3 + "FOUR"
or
"THREE" + "FOUR"

What would you expect to happen? Numerical addition might if the language
in which it is written does enuff magic for you. But this is really black
magic: turning strings into numbers is like turning people into toads. It
should only happen when you really want it to!

Try this:
3 * "THREE"

Python is dynamically but strongly typed. This means that while you can
easily change the type of an object you must do this explicitly and you can
never do things with an object which its object type cannot do. This makes
programming Python both remarkably simple and remarkably safe. You can have
all kinds of problems if the computer does something automagically for you.

Amitiés

Charlie