Python Forum

Full Version: Position after given movements
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everybody,

I have a task that I don't quite understand how to tackle.

At the moment I have written this code but I do not get the result that it should give, I leave the code:
movement = str(input("Write movements: "))
m = list(movement)
lon = len(movement)
Turnleft = 0
Turnright = 0
Up = 0
Down = 0
Leftside = 0
Rightside = 0

for i in range(lon):
	p = m[i]
	if i == 0 and p == "F":
		Turnright += 1
	elif  p == "F":
		if m[i-1] == "L":
			Turnleft += 1
		if m[i-1] == "R":
			Turnright += 1
		if m[i-1] == "U":
			Up += 1
		if m[i-1] == "D":
			Down += 1
		if m[i-1] == "X":
			Leftside += 1
		if m[i-1] == "Y":
			Rightside += 1

	x = Turnright - Turnleft
	y = Up - Down
	z = Leftside - Rightside
	print(x, y, z)
If anyone can fix the code for me, I appreciate it.

TASK LINK
Quote:but I do not get the result that it should give
What do you get, and what are you expecting?
(Apr-18-2021, 08:43 PM)Larz60+ Wrote: [ -> ]
Quote:but I do not get the result that it should give
What do you get, and what are you expecting?

I put the link to the task, there you can check what I need to get as output.
Then in my program if I write the exaple FRFDFYFLF the output is:
1 0 0
1 0 0
2 0 0
2 0 0
2 -1 0
2 -1 0
2 -1 -1
2 -1 -1
1 -1 -1

As you can see is wrong since line 3 (2, 0, 0) but I dont understand why. The correct output should be:
1 0 0
1 0 0
1 1 0
1 1 0
1 1 -1
1 1 -1
1 1 -2
1 1 -2
1 2 -2
I don't like your logic. Why don't you use just 3 variables reflecting the 3 dimensions, rather than 6? Would it not make sense that LeftRight could be incremented if turn right, decremented if turn left, rather than using the difference between the 2?