[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

I just started trying to learn Python. I was following some tutorials

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 12
Thread images: 1

File: pythonlogo.jpg (46KB, 680x459px) Image search: [Google]
pythonlogo.jpg
46KB, 680x459px
I just started trying to learn Python. I was following some tutorials that showed a simple way for finding prime numbers. I decided to modify it a bit to just try and output whether numbers in a set were divisible by 3. This is what I came up with.

for n in range(3, 13):
for x in range(3, n):
if n % x >= 1:
print(n, 'Is not divisible by 3')
break
else:
print(n, 'is divisible by 3')

Unfortunately, this is only showing 3 as being divisible by 3, and 4-12 not being divisible by 3. Can anyone explain where I'm going wrong?
>>
why do you have a break there?
>>
I copied straight from the Idle, so it isn't preserving the indentation in the post, I guess.
>>
>>55966048
Because the function loops and gives the outputs for each number several times without it
>>
it's in python
>>
While I'm still curious what I did wrong, there was a much simpler way of doing it, it seems.

for n in range(3, 13):
if n % 3 >= 1:
print(n, 'Is not divisible by 3')
continue
print(n, 'is divisible by 3')
>>
use [ code ] [ /code ] tags (remove the spaces) to preserve whitepsace.

i'm not going to bother trying to understand your thought-process in the code you've written but the proper way to tell if something is divisible by 3 is to check that x % 3 == 0

a cool way of selecting all of the elements in an iterable that are divisible by 3 is with list comprehensions

my_thing = range(3, 13)
[n for n in my_thing if n % 3 == 0]
>>
I think I found your example, and if you followed that examples indentation then your else is on the for loop and not the if statement. In that case it wouldn't catch anything higher than 3 because your weren't just checking x, you were checking from 3 to x and 4 will break that for loop for each of those iterations
>>
>>55966026
look at why it shows 6 as not being divisible by 3. when x = 4 in the inner loop, 6 divided by 4 leaves a remainder of 2, which is greater than 1. so, '6 is not divisible by 3' will be printed.
>>
>>55966176
This is still to complicated. Without getting super pythonic and flying into generators or comprehensions you'd be better off using an else there rather than a continue.
>>
>>55966176
use n%3 == 0 instead of n%3 >= 1 to check if n is divisible by 3. the modulo operation in python can return a negative number and is not consistent with euclidean division.
>>
>>55966230
Well, my thought process was a bit off, since I started with code from for prime numbers. Initially, I was having it check for remainder being equivalent to 0, but it still wasn't coming out right. I suppose I was ultimately just making things way too complicated.
Thread posts: 12
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.