[Tutor] Starting over with Python
Luke Paireepinart
rabidpoobear at gmail.com
Thu Dec 14 08:27:50 CET 2006
John Carmona wrote:
> After quite a while away from Python, I have decided to re-study Python. I
> am interested to learn Python to support my love for Cryptography. I have a
> first very easy question (did some search on Google but could not find
> anything helpful). I realise that this is very basic so be gentle with me.
>
> If i insert the following script:
>
> -------------------------------------------------------------------------------------------------
> odd =1
>
>>>> while odd <=100:
>>>>
> if (odd%2)==1:
> print odd
> odd = odd + 1
> -------------------------------------------------------------------------------------------------
> I get a list of the odd numbers from 1 to 99. But now if I wanted to add
> those number together (i.e. 1 + 3 +5 + 7 etc.), what line of coding should I
> include? I have tried to add "odd + odd" but it did not work. In advance
> thanks.
>
Odd is the variable you're iterating over.
If you added odd+odd it would just do something weird, like counting
1
2
4
8
16
32
etc.
Uh...
What you need to do is have a separate variable that stores the sums
and add to that each time.
The problem is since you're iterating over odd, you're changing what the
'if (odd%2)' statement is seeing, if you change odd +1 to odd+odd.
Hope that helps,
-Luke
> If anyone could direct me to some site where python is associated with
> Cryptography I would be very grateful. Many thanks
> JC
>
> _________________________________________________________________
> Be the first to hear what's new at MSN - sign up to our free newsletters!
> http://www.msn.co.uk/newsletters
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list