RE: [Tutor] loop problem
by BELSEY, Dylan other posts by this author
Jun 11 2002 5:35AM messages near this date
[Tutor] Re: parsing--is this right?
|
RE: [Tutor] Re: Newbie OOP Question.
(diff between function, modu le and class)
I don't have Alan's book to see what you are referring to but have a
look at the following. I think it is a solution. From what I can
ascertain, you are trying to find the result for 2^30 = 1073741824 ?? The
following code should do it. You don't have to use the index of the for
loop and must reset "b" at the start. Also, if you only want the final
result then print outside the loop.
Hope this helps.
PS: just observed that Sean Perry had the same idea!!
> >> b = 1
> >> for i in range(1,31):
b = b*2
print b
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
> >>
-----Original Message-----
From: Tinman [mailto:mikew@[...].com]
Sent: Tuesday, 11 June 2002 09:42
To: tutor@[...].org
Subject: [Tutor] loop problem
I've been reading Alan's book on learning to program with Python. I'm
currently working with the chapter on loops. I have an idea for a loop
that I can't quite figure out. This is what I want to do.
1.Multiply 1 by 2
2.Take the result and multiply by 2
3.So on and so on thirty times
4.Print the final result
I tried this:
for i in range(1,31):
b = i * 2
print b
This prints 1 -30 times 2 as I'm sure you can tell. I can't figure out
how to take each result an double it. Can anyone help a hapless Python
newbie?
_______________________________________________
Tutor maillist - Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
|