Search code examples
pythonvariablesoutputcomparison

why assigning a value to an variable doesn't work here?


I did a programming challenge in codewars, I got stuck, and I have no clue why I this code deosn't give me the results I desire. here is my code:

     def magic_plant(p_field,split,n):
       b=n
       while(n>0):
          p_field=p_field[:-2]
          if(p_field=="o"):
              break
          n=n-1
       z=p_field*(split^b)
       return(z) 
    print(magic_plant("o\n|\n|",3,3))

here is the challenge, it explains it way clear than I would: https://www.codewars.com/kata/6339de328a3b8f0016cc5b8d my intention from this code is to provide to the variable z the variables with the given values:

b = n = 3
split = 3 
p_field = "o"

and as a result, idealy print z and thus get the output:

ooooooooooooooooooooooooooo

(27 o's, as per the calculation) instead my output is nothing: I tried to see what is the root of the problem, and eventually came to realize that the bug lies at line 2 (b=n). if I neglect that line, then I get o as an output. what is going on here? why do I get such results in the first place- for b=n, and o for not writing b=n?


Solution

  • split^b should be split**b. ^ is the XOR operator in python, not the exponent operator.