| | |
Changing For loops to while loops and vice versa
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 42
Reputation:
Solved Threads: 0
I'm struggling on how to do this.
For example.
i think i need to understand what this does more.
Firstly what does % mean?
if i is the square route of 3?
i've attempted the question but i think i'm a while off because the example i worked to help me dont seem to be anything like this one.
i came up with:
but as you can see i'm really just changing little bits and i suspect i'll be needing to change most of it.
Thanks in advance, and try to keep things simple for me please ^^
For example.
Python Syntax (Toggle Plain Text)
for i in range (1 ,100): if i % 3 == 2: print i, " mod ", 3, "= 2"
i think i need to understand what this does more.
Firstly what does % mean?
if i is the square route of 3?
i've attempted the question but i think i'm a while off because the example i worked to help me dont seem to be anything like this one.
i came up with:
Python Syntax (Toggle Plain Text)
def int(start,finish): while i in range(1,100): i % 3 == 2; print i, "mod", 3, "=2";
but as you can see i'm really just changing little bits and i suspect i'll be needing to change most of it.
Thanks in advance, and try to keep things simple for me please ^^
in the first code the
means:
if the remainder of i/3 is = to 2:
Python Syntax (Toggle Plain Text)
if i % 3 == 2:
if the remainder of i/3 is = to 2:
don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
Now to the difference between for and while. while loops test a condition and execute the body as long as that condition is True. Normally, within the body of the while loop you would have to modify some variables to make the condition false eventually (in this case, you may increment the variable i so that eventually it is not in the range (1, 100)).
For loops are a customized version of the while loop that does this incrementation for you. That is, it automatically initializes a variable and changes it, executing the body of the loop until the condition evaluates False. In this case, the for loop creates the variable i and automatically changes its value to the next number in the range (1, 100) for every subsequent iteration of the loop.
I hope I didn't confuse you more than I helped you out
For loops are a customized version of the while loop that does this incrementation for you. That is, it automatically initializes a variable and changes it, executing the body of the loop until the condition evaluates False. In this case, the for loop creates the variable i and automatically changes its value to the next number in the range (1, 100) for every subsequent iteration of the loop.
I hope I didn't confuse you more than I helped you out
Last edited by scru; Apr 10th, 2009 at 1:13 pm.
Here % is the modulo operator. It gives you the remainder of a division.
Another thing, in your function you came up with -->
using int would be a solid nono, since int() is a function built into Python and you would make that fuction inoperable!
python Syntax (Toggle Plain Text)
print(3 % 3) # 0 --> 3 is completely divisable by 3 print(6 % 3) # 0 --> 6 is completely divisable by 3 print(5 % 3) # 2 --> division leaves a ramainder of 2
Python Syntax (Toggle Plain Text)
def int(start,finish):
Last edited by sneekula; Apr 10th, 2009 at 1:25 pm.
No one died when Clinton lied.
•
•
Join Date: Jan 2009
Posts: 42
Reputation:
Solved Threads: 0
hey guys,
sorry for the lack of reply after the helpful help.
i believe i got it done, but i do have one simple question.
it returns the correct answer.
Is there something i can put there instead of that bold print so it does nothing, since this leaves 1 line between each modulo answer.
I did try taking print out but then i has problems.
haha is there a print nothing! function.
if not then this is still good enough, i'd like to thank you guys for the help anyway, i am becoming quite used to python basics now.
and its all thanks to you geezers!
Cheers ^^
sorry for the lack of reply after the helpful help.
i believe i got it done, but i do have one simple question.
i=0
while i < 101:
if i%3 == 2:
print i, "mod", 3, "=2"
else:
print
i = i+1it returns the correct answer.
Is there something i can put there instead of that bold print so it does nothing, since this leaves 1 line between each modulo answer.
I did try taking print out but then i has problems.
haha is there a print nothing! function.
if not then this is still good enough, i'd like to thank you guys for the help anyway, i am becoming quite used to python basics now.
and its all thanks to you geezers!
Cheers ^^
Last edited by thehivetyrant; Apr 12th, 2009 at 9:56 am. Reason: clarity
![]() |
Other Threads in the Python Forum
- Previous Thread: Changing values?
- Next Thread: python stopwatch
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






