Changing For loops to while loops and vice versa

Thread Solved

Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster

Changing For loops to while loops and vice versa

 
0
  #1
Apr 10th, 2009
I'm struggling on how to do this.
For example.
  1. for i in range (1 ,100):
  2. if i % 3 == 2:
  3. 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:
  1. def int(start,finish):
  2. while i in range(1,100):
  3. i % 3 == 2;
  4. 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 ^^
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 395
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: Changing For loops to while loops and vice versa

 
0
  #2
Apr 10th, 2009
in the first code the
  1. if i % 3 == 2:
means:
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,603
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Changing For loops to while loops and vice versa

 
0
  #3
Apr 10th, 2009
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
Last edited by scru; Apr 10th, 2009 at 1:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Changing For loops to while loops and vice versa

 
0
  #4
Apr 10th, 2009
Here % is the modulo operator. It gives you the remainder of a division.
  1. print(3 % 3) # 0 --> 3 is completely divisable by 3
  2. print(6 % 3) # 0 --> 6 is completely divisable by 3
  3. print(5 % 3) # 2 --> division leaves a ramainder of 2
Another thing, in your function you came up with -->
  1. def int(start,finish):
using int would be a solid nono, since int() is a function built into Python and you would make that fuction inoperable!
Last edited by sneekula; Apr 10th, 2009 at 1:25 pm.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster

Re: Changing For loops to while loops and vice versa

 
0
  #5
Apr 12th, 2009
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.

i=0
while i < 101:
	if i%3 == 2:
		print i, "mod", 3, "=2"
	else:
		print
	i = i+1

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 ^^
Last edited by thehivetyrant; Apr 12th, 2009 at 9:56 am. Reason: clarity
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,603
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Changing For loops to while loops and vice versa

 
1
  #6
Apr 12th, 2009
You can take out the entire else part of the statement statement; the else and elif parts of an if statement are optional.

Another way is the pass keyword. Use it to get out of a block that you intend to do nothing in.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster

Re: Changing For loops to while loops and vice versa

 
0
  #7
Apr 12th, 2009
Excellent, the exact 2 answers i was looking for!

lets consider this thread Solved!!!!!!!!!!

thanks!
Last edited by thehivetyrant; Apr 12th, 2009 at 12:39 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC