A
aaB
hello,
I am a beginner programmer. I started learning programming about a year and a
half ago, using C. I picked up python a few months ago, but only wrote very few
scripts.
I am currently trying to learn more about the python way of doing things by
writing a script that generates png images using a 1D cellular automaton.
While writing preliminary code for that project, I ran into a behaviour that I
don't understand.
I am using python 2.7 on a linux system.
I represent the CA's rule with a list of integers, of value 1 or 0.
Here is the function I use to generate the list:
def get_rule(rulenum):
rule = []
while rulenum > 0:
rule.append(rulenume % 2)
rulenum /= 2
while len(rule) < 8:
rule.append(0)
rule.reverse()
return rule
if i call it by writing:
rule = getrule(int(8))
and then call:
print rule
the output is good:
[0, 0, 0, 0, 1, 0, 0, 0]
I then tried to print each item of the list using a for loop:
for i in range(rule):
print rule
the output is, as expected:
0
0
0
0
1
0
0
0
but when I do:
for i in rule:
print rule
I get the "complement":
1
1
1
1
0
1
1
1
There must be something I didn't understand correctly in the for statement, but
I really can't think of a reason why the output is what it is.
I tried this using the interactive console, and the results are the same,
whatever the length of the list, i always get the complement of my bit pattern.
Any pointers to help me understand this would be appreciated.
I am also running into an other issue with this script, but I'd rather
understand this before asking further questions.
Thanks, and sorry for the rather long post.
I am a beginner programmer. I started learning programming about a year and a
half ago, using C. I picked up python a few months ago, but only wrote very few
scripts.
I am currently trying to learn more about the python way of doing things by
writing a script that generates png images using a 1D cellular automaton.
While writing preliminary code for that project, I ran into a behaviour that I
don't understand.
I am using python 2.7 on a linux system.
I represent the CA's rule with a list of integers, of value 1 or 0.
Here is the function I use to generate the list:
def get_rule(rulenum):
rule = []
while rulenum > 0:
rule.append(rulenume % 2)
rulenum /= 2
while len(rule) < 8:
rule.append(0)
rule.reverse()
return rule
if i call it by writing:
rule = getrule(int(8))
and then call:
print rule
the output is good:
[0, 0, 0, 0, 1, 0, 0, 0]
I then tried to print each item of the list using a for loop:
for i in range(rule):
print rule
the output is, as expected:
0
0
0
0
1
0
0
0
but when I do:
for i in rule:
print rule
I get the "complement":
1
1
1
1
0
1
1
1
There must be something I didn't understand correctly in the for statement, but
I really can't think of a reason why the output is what it is.
I tried this using the interactive console, and the results are the same,
whatever the length of the list, i always get the complement of my bit pattern.
Any pointers to help me understand this would be appreciated.
I am also running into an other issue with this script, but I'd rather
understand this before asking further questions.
Thanks, and sorry for the rather long post.