I bet that half the questions here are about that.
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;
}
You have to update Thorâs position when you move.
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.
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.
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
Your break keyword seems badly placed.
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.
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)
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.
-
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)
-
We are not using any debugger (the tool) here. We are just inserting print statements to help understand the code (and debug it).
-
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 ) = ("
. -
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.
-
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"
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.
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).
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
is there a way for me to make my own practice test so that I can test features of my code on the website ?
- 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.
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.
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.
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
No. Good luck