# Power Of Thor - Episode 1 - Puzzle discussion

#611

I bet that half the questions here are about that.

#612

Why my code doesnât work? C

int main() {
int lightX, lightY, initialTX, initialTY;
scanf("%d%d%d%d", &lightX, &lightY, &initialTX, &initialTY);
while (1) {
int remainingTurns;
scanf("%d", &remainingTurns);
// N NE E SE S SW W or NW
if (initialTX < lightX)
printf(âE\nâ);
if (initialTX > lightX)
printf(âW\nâ);
if (initialTY < lightY)
printf(âN\nâ);
if (initialTY > lightY)
printf(âS\nâ);
}

return 0;

}

#613

You have to update Thorâs position when you move.

#614

Hi, new here im having trouble understanding what im doing wrong. I dont want to look at the answer. Would it be possible for someone to take a look at my code and tell me? I am new to python so I am not sure if I made a simple mistake or maybe there is something that I just dont know im missing.

#615

What IS wrong?
Do you get an error or you just donât manage to bring Thor to the light of power?

I suggest you try to explain (in writing or out loud) how you tried to solve the puzzle: what inputs youâre taking into account, how you find where Thor should go, how do you update your variables, what you pass in output.

You might find this annoying but Iâm pretty sure you can find what is wrong by yourself.

#616

well I get several errors when I try to run the program first error has to do with âif thory > light_y:â it says not in a function. This is my current code setup. This does not work starting on line 29.
[âŚ]
while True:
remaining_turns = int(input()) # The remaining amount of turns Thor can move. Do not remove this line.

# Write an action using print
# To debug: print("Debug messages...", file=sys.stderr)

if thory > light_y:
directiony = "N"
thory = thory =-1
elif thory < light_y:
directiony = "S"
thory = thory =+1
if thorx > light_x:
directionx = "W"
thorx = thorx =-1
elif thorx < light_x:
directiony = "E"
thorx = thorx =+1
break
print(directiony + directionx)

# A single line providing the move to be made: N NE E SE S SW W or NW

#617

#618

Alright so I figured it out. Forgot about how indentation effects loops etc. I still have a problem with no explanation. Why does thor not move? I get no error on this so I dont know whats wrong.

Apparently that was also an indentation issue.
so on the 2nd test thor goes north 1 round then goes south not sure why.Using the same code as above just removed break and adjusted indentation.

#619

You canât say that âThor doesnât moveâ because of an âindentation problemâ. Of course it doesnât! If your program doesnât compile, the whole game doesnât start and you get a compilation message instead which precisely explain to you what is your problem.

Once your program has successfully compiled, but doesnât do what you expect, you need to understand what is happening. You can stare forever at your screen hoping to spot your problemâŚ or you can ask your program for some clues instead. Just insert debugging statements into your code to gather useful informations. Per instance :

turn = 0
while True:
turn += 1
remaining_turns = int(input()) # The remaining amount of turns Thor can move. Do not remove this line.

# Write an action using print
# To debug: print("Debug messages...", file=sys.stderr)

print(turn, "- ( thorx, thory ) = (", thor_x, ",", thor_y, ")", file=sys.stderr)
print(turn, "- ( light_x, light_y ) = (", light_x, ",", light_y, ")", file=sys.stderr)

if thory > light_y:
directiony = "N"
thory = thory =-1 # Deaf programming?
elif thory < light_y:
directiony = "S"
thory = thory =+1
else:
print("Newton's first law does apply to programming too.")
if thorx > light_x:
directionx = "W"
thorx = thorx =-1
elif thorx < light_x:
directiony = "E"
thorx = thorx =+1

print(turn, "- ( directionx, directiony ) = (", directionx, ",", directiony, ")", file=sys.stderr)
print(directiony + directionx)

#620

scratch this, just ran the code its easy enough to understand now. The debugger doesnt explain what im doing wrong going by the code it should continue to go north. Does the elif statement activate even if thory is not < light_y ?

what does the - do at the beginning? Also if the first set of ([quote=âAries1, post:619, topic:30â]
( light_x, light_y )
[/quote] is for the debugging what is the second part[quote=âAries1, post:619, topic:30â]
= (", light_x, â,â, light_y, â)â
[/quote]

for ? Super new to Python so I dont fully understand what is put in place.

#621
1. My bad. Iâve made two errors in my example which prevent it to compile (thor_x/y instead of thorx/y and missing âfile=sys.stderrâ parameter).

turn = 0
while True:
turn += 1
remaining_turns = int(input()) # The remaining amount of turns Thor can move. Do not remove this line.

# Write an action using print
# To debug: print(\"Debug messages...\", file=sys.stderr)

print(turn, "- ( thorx, thory ) = (", thorx, ",", thory, ")", file=sys.stderr)
print(turn, "- ( light_x, light_y ) = (", light_x, ",", light_y, ")", file=sys.stderr)

if thory > light_y:
directiony = "N"
thory = thory =-1
elif thory < light_y:
directiony = "S"
thory = thory =+1
else:
print("Newton's first law does apply to programming too.", file=sys.stderr)
if thorx > light_x:
directionx = "W"
thorx = thorx =-1
elif thorx < light_x:
directiony = "E"
thorx = thorx =+1

print(turn, "- ( directionx, directiony ) = (", directionx, ",", directiony, ")", file=sys.stderr)
print(directiony + directionx)

2. We are not using any debugger (the tool) here. We are just inserting print statements to help understand the code (and debug it).

3. Things inside double quotes are "string", not Python code. Seriously, you know this. When you write 'directiony = "N"', do you really think âNâ is a magical Python statement? Itâs obviously the same with "- ( thorx, thory ) = (".

4. Now, try to insert âdebug statementsâ which are just instructions-which-do-nothing-beyond-printing-useful-information-on-error-output, run you code and try to find the 3 different types of error your code contains at this time.

5. To make things clearer, the following code:

a = 2
b = âcoinâ
print( âcanardâ, a, b, b, file=sys.stderr)

Print the following on the error output:

"canard 2 coin coin"

#622

I figured that out shortly after running the code. Also fixed the issues with having it compile. Ill get to work on the debug statements hopefully I can figure it out.

ps thanks very much for taking the time with helping me understand.

#623

Nice! You should now be able to tackle the 3 bugs standing between Thor and its goal. One of them just arises from a misleading syntax, another is a nasty typo error (the more you look at these, the less you are able to spot themâŚ), whereas the last is a logical flaw (or a misunderstanding of a variable scope, which could easily be overlooked in a language such as Python).

#624

Finally figured it out. Wish I knew why I didnt see that before. Thanks again for the help.

New problem only have the last practical left but for some reason it wont run the statement for this program im wondering if this code is wrong everything else works. When the program finishes its runs it says the program didnt send any directions.

elif thory and thorx < light_y and light_x:
print ("SE")
thory, thorx = thory +1, thorx + 1

#625

is there a way for me to make my own practice test so that I can test features of my code on the website ?

#626
• if you mean writing a specific test for an existing puzzle:
Yes! I just wrote about it in the latest blog post (tip #6)
• if you mean writing your own puzzle and tests:
Yes! You can craft your own puzzles in the contribute section.

#627

Do I have to beat the test or raise my status before I can utilize that ability ? I dont see those buttons right now im only lvl 2.

#628

hey, Iâm sorry, I replied a bit too quickly. This feature exists only for In/Out puzzles, so not for Power of Thor which is a puzzle with a game loop.

#631

I have to do this for school but ive got no clue
can u send me a copy paste solution or something like that
[lolno]

thanks

#632

No. Good luck