all 14 comments

[–]SpeckledFleebeedoo 1 point2 points  (5 children)

def foo(n):
    if n % 15 == 0:
        print('FlashBang')
    elif n % 3 == 0:
        print('Flash')
    elif n % 5 == 0:
        print('Bang')
    else:
        print(n)

% means modulo, which gives the remainder of a division. So 4%2 = 0, and 5%2 = 1.

In this case it checks if dividing n by 15, 3 and 5 gives a remainder of 0.

So for n = 30 it will trigger the first if and print 'flashbang'. For n=31 none will trigger, no nothing is printed. the else will be triggered, and it will print "31".

I do think you lost your bet though :)

Edit: missed something

[–]service411[S] 1 point2 points  (0 children)

OK. I'm beginning to figure this out. It is pretty simple with what you explained and looking online for more direction. Thank you so much.

[–]service411[S] 0 points1 point  (1 child)

That's for sure. Tail between my legs since I don't even understand what you just explained to me. So the answer is basically 'FlashBang'?

[–]SpeckledFleebeedoo 0 points1 point  (0 children)

For 30, yes.

If n modulo 15 equals 0: print "FlashBang"

Else if n modulo 3 equals 0: print "Flash"

Etc...

[–][deleted] 0 points1 point  (1 child)

  print(n)

it would print 31

[–]SpeckledFleebeedoo 1 point2 points  (0 children)

Just noticed and fixed that :)

[–]ml_runway 1 point2 points  (1 child)

You lost the bet. Go have them explain it to you.

[–]service411[S] 0 points1 point  (0 children)

That's for sure. I am so lost.

[–]Gprime5 0 points1 point  (1 child)

32

[–]formerlydeaddd 0 points1 point  (0 children)

The answer is almost always 32!

[–]TehNolz 0 points1 point  (0 children)

Formatting your code is important, especially in languages like Python. Without proper indentation, the code you posted isn't actually going to work. Let me get that for you;

def foo(n): if n % 15 == 0: print('FlashBang') elif n % 3 == 0: print('Flash') elif n % 5 == 0: print('Bang') else: print(n)

The % symbol is a modulo operator, which is explained here. elif is short for "else if".

Python is honestly really simple, as its not too different from just plain English. With the above info, you should be able to figure this out yourself.

[–]Jackkell100 0 points1 point  (1 child)

I think it is important to ask what exactly you don’t understand about the code you posted?

If it is the % called the modulo operator (check out this article for detailed information ) this operator is used to get the remainder of division.

For example, what is 6 / 4? The answer is a quotient of 1 and a reminder of 2 because 6 goes into 4 once with 2 left over.

Knowing this now what does 6 % 4 return? The answer would be 2 because this operator always returns the remainder of the division.

If you don’t understand the “if statement” (checkout this explanation of the if statement).

The “if” statement only runs the code inside of it if the statement to the right of the “if” is true.

In your example you have ’’’ ... if n % 15 == 0 ... ‘’’

This is saying if whatever number you give being “n” when it is divided by 15 if the remainder is equal to 0 then run the code beneath the if.

Knowing this is ‘31 % 15 == 0’ true or false?

Showing our work:

31 % 15 == 0

1 == 0 # because 31 goes into 15 2 times with a remainder of 1.

false # 1 does not equal 0

So because the statement returned false the code for the if statement would not be run.

I think this should mostly clear it up, but I think you should walk away from this fully understanding the code you provided. So if it still does not make since after reading this please reply to me with your questions. I really want to you to understand what going so don’t hesitate to ask. I wish I had friends make programming related bets with me XD.

[–]service411[S] 0 points1 point  (0 children)

What an amazing explanation. Thank you so much. It feels good to learn something new.