cubeclick = "b"
def clicksave(type):
clickcheck = ["", "", ""]
for i in range(type):
if cubeclick != clickcheck[i-1]:
clickcheck[i] = cubeclick
elif clickcheck[0] == "":
if cubeclick != "":
clickcheck[0] = cubeclick
else:
while cubeclick == "":
pass
continue
return clickcheck
print(clicksave(3))
This code treats incoming connections from three sides of the cube as A, B, and C, respectively, and returns them in a list format. But the results are not good.
['b', '', 'b']
Why are the results of this program like this?
CodePudding user response:
cubeclick = "b"
def clicksave(type):
clickcheck = ["", "", ""]
for i in range(type):
if cubeclick != clickcheck[i]:
clickcheck[i] = cubeclick
print(clickcheck[i])
elif clickcheck[0] == "":
if cubeclick != "":
clickcheck[0] = cubeclick
print(clickcheck[0])
else:
while cubeclick == "":
pass
continue
return clickcheck
print(clicksave(3))
Try this
CodePudding user response:
If you unroll the loop and replace cubeclick
with "b"
, you see why the results are like that:
clickcheck = ["", "", ""]
if "b" != clickcheck[-1]: # true
clickcheck[0] = "b" # Do this
elif clickcheck[0] == "":
if "b" != "":
clickcheck[0] = "b"
# The else: branch never does anything.
# clickcheck is ["b", "", ""]
if "b" != clickcheck[0]: # false
clickcheck[1] = "b"
elif clickcheck[0] == "": # true
if "b" != "": # true
clickcheck[0] = "b" # Do this
# clickcheck is still ["b", "", ""]
if "b" != clickcheck[1]: # true
clickcheck[2] = "b" # Do this
elif clickcheck[0] == "":
if "b" != "":
clickcheck[0] = "b"
# clickcheck is ["b", "", "b"]
It's very difficult to give any advice about how to change anything, as it is very unclear what the result is supposed to be.