I'm struggling on how to do this.
For example.

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:

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 ^^

Member Avatar for leegeorg07

in the first code the

if i % 3 == 2:

means:
if the remainder of i/3 is = to 2:

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 :P

Here % is the modulo operator. It gives you the remainder of a division.

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

Another thing, in your function you came up with -->

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!

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:
		[B]print[/B]
	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 ^^

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.

Excellent, the exact 2 answers i was looking for!

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

thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.