Turtle Graphics path using a string
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Edit: I managed to fix it.
...
I don't know too much about Python but I'm trying to write a function that uses Python's Turtle Graphics to draw a simple path based on the characters in the string.
So, for example, if string = "FRRL" then the turtle should move forward, right, right, left.
When I run this code:
import turtle
step = 100
angle = 90
t = turtle.Turtle()
t.forward(step)
t.left(angle)
t.right(angle)
turtle.done()
It gives a different output to the one I am trying to make below:
import turtle
t = turtle.Turtle()
S = "FLR"
step = 100
angle = 90
for i in S:
if i == 'F' or 'E':
t.forward(step)
if i == 'L':
t.left(angle)
if i == 'R':
t.right(angle)
turtle.done()
The code runs but it seems that in this one it moves the turtle the direction it's facing (so forward I guess) on top of what the if i == '...': t....(angle/step) is telling it to do. So, for example if i == 'R', it will move it forward first and then turn it by 90 degrees to the right, instead of just turning it - same for i == 'F' and i == 'L'. It moves all of them forward first before carrying out the turtle move I want it to.
How do I fix this? Thanks.
python
|
show 1 more comment
Edit: I managed to fix it.
...
I don't know too much about Python but I'm trying to write a function that uses Python's Turtle Graphics to draw a simple path based on the characters in the string.
So, for example, if string = "FRRL" then the turtle should move forward, right, right, left.
When I run this code:
import turtle
step = 100
angle = 90
t = turtle.Turtle()
t.forward(step)
t.left(angle)
t.right(angle)
turtle.done()
It gives a different output to the one I am trying to make below:
import turtle
t = turtle.Turtle()
S = "FLR"
step = 100
angle = 90
for i in S:
if i == 'F' or 'E':
t.forward(step)
if i == 'L':
t.left(angle)
if i == 'R':
t.right(angle)
turtle.done()
The code runs but it seems that in this one it moves the turtle the direction it's facing (so forward I guess) on top of what the if i == '...': t....(angle/step) is telling it to do. So, for example if i == 'R', it will move it forward first and then turn it by 90 degrees to the right, instead of just turning it - same for i == 'F' and i == 'L'. It moves all of them forward first before carrying out the turtle move I want it to.
How do I fix this? Thanks.
python
For me your second code works just fine
– Tobias Wilfert
Nov 23 '18 at 18:48
@TobiasWilfert The code runs but it doesn't draw the same thing as the first one, so I assume it's not correct? For me it always moves it forward first before doing a turtle move I want it to.
– Mandingo
Nov 23 '18 at 18:49
Well, it checks ever 'char' in S and the first char is F so it moves forward then turns left and right.
– Tobias Wilfert
Nov 23 '18 at 18:51
please consider adding 2 pictures because for me both codes give exactly the same outcome.
– Tobias Wilfert
Nov 23 '18 at 18:52
@TobiasWilfert This is what I get when I use the string = "FLR": prnt.sc/lmbbho and this is what I get when I do it the other way: prnt.sc/lmbbj0 - When i use the string it always moves it forward first for me and then does the "F" "L" or "L" after moving it forward first, I don't know why and I don't want it to move forward each time. Any ideas why I don't get the same output?
– Mandingo
Nov 24 '18 at 5:43
|
show 1 more comment
Edit: I managed to fix it.
...
I don't know too much about Python but I'm trying to write a function that uses Python's Turtle Graphics to draw a simple path based on the characters in the string.
So, for example, if string = "FRRL" then the turtle should move forward, right, right, left.
When I run this code:
import turtle
step = 100
angle = 90
t = turtle.Turtle()
t.forward(step)
t.left(angle)
t.right(angle)
turtle.done()
It gives a different output to the one I am trying to make below:
import turtle
t = turtle.Turtle()
S = "FLR"
step = 100
angle = 90
for i in S:
if i == 'F' or 'E':
t.forward(step)
if i == 'L':
t.left(angle)
if i == 'R':
t.right(angle)
turtle.done()
The code runs but it seems that in this one it moves the turtle the direction it's facing (so forward I guess) on top of what the if i == '...': t....(angle/step) is telling it to do. So, for example if i == 'R', it will move it forward first and then turn it by 90 degrees to the right, instead of just turning it - same for i == 'F' and i == 'L'. It moves all of them forward first before carrying out the turtle move I want it to.
How do I fix this? Thanks.
python
Edit: I managed to fix it.
...
I don't know too much about Python but I'm trying to write a function that uses Python's Turtle Graphics to draw a simple path based on the characters in the string.
So, for example, if string = "FRRL" then the turtle should move forward, right, right, left.
When I run this code:
import turtle
step = 100
angle = 90
t = turtle.Turtle()
t.forward(step)
t.left(angle)
t.right(angle)
turtle.done()
It gives a different output to the one I am trying to make below:
import turtle
t = turtle.Turtle()
S = "FLR"
step = 100
angle = 90
for i in S:
if i == 'F' or 'E':
t.forward(step)
if i == 'L':
t.left(angle)
if i == 'R':
t.right(angle)
turtle.done()
The code runs but it seems that in this one it moves the turtle the direction it's facing (so forward I guess) on top of what the if i == '...': t....(angle/step) is telling it to do. So, for example if i == 'R', it will move it forward first and then turn it by 90 degrees to the right, instead of just turning it - same for i == 'F' and i == 'L'. It moves all of them forward first before carrying out the turtle move I want it to.
How do I fix this? Thanks.
python
python
edited Nov 24 '18 at 9:25
Mandingo
asked Nov 23 '18 at 18:43
MandingoMandingo
376212
376212
For me your second code works just fine
– Tobias Wilfert
Nov 23 '18 at 18:48
@TobiasWilfert The code runs but it doesn't draw the same thing as the first one, so I assume it's not correct? For me it always moves it forward first before doing a turtle move I want it to.
– Mandingo
Nov 23 '18 at 18:49
Well, it checks ever 'char' in S and the first char is F so it moves forward then turns left and right.
– Tobias Wilfert
Nov 23 '18 at 18:51
please consider adding 2 pictures because for me both codes give exactly the same outcome.
– Tobias Wilfert
Nov 23 '18 at 18:52
@TobiasWilfert This is what I get when I use the string = "FLR": prnt.sc/lmbbho and this is what I get when I do it the other way: prnt.sc/lmbbj0 - When i use the string it always moves it forward first for me and then does the "F" "L" or "L" after moving it forward first, I don't know why and I don't want it to move forward each time. Any ideas why I don't get the same output?
– Mandingo
Nov 24 '18 at 5:43
|
show 1 more comment
For me your second code works just fine
– Tobias Wilfert
Nov 23 '18 at 18:48
@TobiasWilfert The code runs but it doesn't draw the same thing as the first one, so I assume it's not correct? For me it always moves it forward first before doing a turtle move I want it to.
– Mandingo
Nov 23 '18 at 18:49
Well, it checks ever 'char' in S and the first char is F so it moves forward then turns left and right.
– Tobias Wilfert
Nov 23 '18 at 18:51
please consider adding 2 pictures because for me both codes give exactly the same outcome.
– Tobias Wilfert
Nov 23 '18 at 18:52
@TobiasWilfert This is what I get when I use the string = "FLR": prnt.sc/lmbbho and this is what I get when I do it the other way: prnt.sc/lmbbj0 - When i use the string it always moves it forward first for me and then does the "F" "L" or "L" after moving it forward first, I don't know why and I don't want it to move forward each time. Any ideas why I don't get the same output?
– Mandingo
Nov 24 '18 at 5:43
For me your second code works just fine
– Tobias Wilfert
Nov 23 '18 at 18:48
For me your second code works just fine
– Tobias Wilfert
Nov 23 '18 at 18:48
@TobiasWilfert The code runs but it doesn't draw the same thing as the first one, so I assume it's not correct? For me it always moves it forward first before doing a turtle move I want it to.
– Mandingo
Nov 23 '18 at 18:49
@TobiasWilfert The code runs but it doesn't draw the same thing as the first one, so I assume it's not correct? For me it always moves it forward first before doing a turtle move I want it to.
– Mandingo
Nov 23 '18 at 18:49
Well, it checks ever 'char' in S and the first char is F so it moves forward then turns left and right.
– Tobias Wilfert
Nov 23 '18 at 18:51
Well, it checks ever 'char' in S and the first char is F so it moves forward then turns left and right.
– Tobias Wilfert
Nov 23 '18 at 18:51
please consider adding 2 pictures because for me both codes give exactly the same outcome.
– Tobias Wilfert
Nov 23 '18 at 18:52
please consider adding 2 pictures because for me both codes give exactly the same outcome.
– Tobias Wilfert
Nov 23 '18 at 18:52
@TobiasWilfert This is what I get when I use the string = "FLR": prnt.sc/lmbbho and this is what I get when I do it the other way: prnt.sc/lmbbj0 - When i use the string it always moves it forward first for me and then does the "F" "L" or "L" after moving it forward first, I don't know why and I don't want it to move forward each time. Any ideas why I don't get the same output?
– Mandingo
Nov 24 '18 at 5:43
@TobiasWilfert This is what I get when I use the string = "FLR": prnt.sc/lmbbho and this is what I get when I do it the other way: prnt.sc/lmbbj0 - When i use the string it always moves it forward first for me and then does the "F" "L" or "L" after moving it forward first, I don't know why and I don't want it to move forward each time. Any ideas why I don't get the same output?
– Mandingo
Nov 24 '18 at 5:43
|
show 1 more comment
2 Answers
2
active
oldest
votes
From your comments, I guess (and this is bad, as you should have given more details about that) you expect that when the letter is "L" the turtle would turn left based on "angle" AND walk the "step".
So, in this case, you missed the forward walk, and this would be the right addition to do so:
if i == 'L':
t.left(angle)
t.forward(step)
if i == 'R':
t.right(angle)
t.forward(step)
PS: In any case, both versions you posted work the same for me!
No, that's what I don't want. It moves the turtle forward regardless of what the letter is 'F', 'L' and 'R' and THEN does the 'F' 'L' or 'R' operation after moving it forward. I don't want it to move forward every time. Do you have any idea why it does not work the same for me?
– Mandingo
Nov 24 '18 at 5:38
When I use my string = "FLR", I get this: prnt.sc/lmbbho and when I do it the other way I get this output and they're not the same for me: prnt.sc/lmbbj0 - using the string it moves the turtle forward each time before doing the "F", "L" or "R" move and I don't want it to go forward each time. Not sure why it does that. Any ideas?
– Mandingo
Nov 24 '18 at 5:45
1
Nevermind, I fixed it. Thanks for the help anywy.
– Mandingo
Nov 24 '18 at 9:28
1
You're welcome! Glad that I helped somehow. Now I understood that the problem was in the condition if i == "F" or "E", because it'll always be true, and "E" is always true as a condition.
– Luan Naufal
Nov 24 '18 at 12:40
add a comment |
I fixed the problem by changing
if i == 'F' or 'E':
to
if i in ['F', 'E']:
and now it works as it should.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451622%2fturtle-graphics-path-using-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
From your comments, I guess (and this is bad, as you should have given more details about that) you expect that when the letter is "L" the turtle would turn left based on "angle" AND walk the "step".
So, in this case, you missed the forward walk, and this would be the right addition to do so:
if i == 'L':
t.left(angle)
t.forward(step)
if i == 'R':
t.right(angle)
t.forward(step)
PS: In any case, both versions you posted work the same for me!
No, that's what I don't want. It moves the turtle forward regardless of what the letter is 'F', 'L' and 'R' and THEN does the 'F' 'L' or 'R' operation after moving it forward. I don't want it to move forward every time. Do you have any idea why it does not work the same for me?
– Mandingo
Nov 24 '18 at 5:38
When I use my string = "FLR", I get this: prnt.sc/lmbbho and when I do it the other way I get this output and they're not the same for me: prnt.sc/lmbbj0 - using the string it moves the turtle forward each time before doing the "F", "L" or "R" move and I don't want it to go forward each time. Not sure why it does that. Any ideas?
– Mandingo
Nov 24 '18 at 5:45
1
Nevermind, I fixed it. Thanks for the help anywy.
– Mandingo
Nov 24 '18 at 9:28
1
You're welcome! Glad that I helped somehow. Now I understood that the problem was in the condition if i == "F" or "E", because it'll always be true, and "E" is always true as a condition.
– Luan Naufal
Nov 24 '18 at 12:40
add a comment |
From your comments, I guess (and this is bad, as you should have given more details about that) you expect that when the letter is "L" the turtle would turn left based on "angle" AND walk the "step".
So, in this case, you missed the forward walk, and this would be the right addition to do so:
if i == 'L':
t.left(angle)
t.forward(step)
if i == 'R':
t.right(angle)
t.forward(step)
PS: In any case, both versions you posted work the same for me!
No, that's what I don't want. It moves the turtle forward regardless of what the letter is 'F', 'L' and 'R' and THEN does the 'F' 'L' or 'R' operation after moving it forward. I don't want it to move forward every time. Do you have any idea why it does not work the same for me?
– Mandingo
Nov 24 '18 at 5:38
When I use my string = "FLR", I get this: prnt.sc/lmbbho and when I do it the other way I get this output and they're not the same for me: prnt.sc/lmbbj0 - using the string it moves the turtle forward each time before doing the "F", "L" or "R" move and I don't want it to go forward each time. Not sure why it does that. Any ideas?
– Mandingo
Nov 24 '18 at 5:45
1
Nevermind, I fixed it. Thanks for the help anywy.
– Mandingo
Nov 24 '18 at 9:28
1
You're welcome! Glad that I helped somehow. Now I understood that the problem was in the condition if i == "F" or "E", because it'll always be true, and "E" is always true as a condition.
– Luan Naufal
Nov 24 '18 at 12:40
add a comment |
From your comments, I guess (and this is bad, as you should have given more details about that) you expect that when the letter is "L" the turtle would turn left based on "angle" AND walk the "step".
So, in this case, you missed the forward walk, and this would be the right addition to do so:
if i == 'L':
t.left(angle)
t.forward(step)
if i == 'R':
t.right(angle)
t.forward(step)
PS: In any case, both versions you posted work the same for me!
From your comments, I guess (and this is bad, as you should have given more details about that) you expect that when the letter is "L" the turtle would turn left based on "angle" AND walk the "step".
So, in this case, you missed the forward walk, and this would be the right addition to do so:
if i == 'L':
t.left(angle)
t.forward(step)
if i == 'R':
t.right(angle)
t.forward(step)
PS: In any case, both versions you posted work the same for me!
answered Nov 23 '18 at 19:13
Luan NaufalLuan Naufal
52519
52519
No, that's what I don't want. It moves the turtle forward regardless of what the letter is 'F', 'L' and 'R' and THEN does the 'F' 'L' or 'R' operation after moving it forward. I don't want it to move forward every time. Do you have any idea why it does not work the same for me?
– Mandingo
Nov 24 '18 at 5:38
When I use my string = "FLR", I get this: prnt.sc/lmbbho and when I do it the other way I get this output and they're not the same for me: prnt.sc/lmbbj0 - using the string it moves the turtle forward each time before doing the "F", "L" or "R" move and I don't want it to go forward each time. Not sure why it does that. Any ideas?
– Mandingo
Nov 24 '18 at 5:45
1
Nevermind, I fixed it. Thanks for the help anywy.
– Mandingo
Nov 24 '18 at 9:28
1
You're welcome! Glad that I helped somehow. Now I understood that the problem was in the condition if i == "F" or "E", because it'll always be true, and "E" is always true as a condition.
– Luan Naufal
Nov 24 '18 at 12:40
add a comment |
No, that's what I don't want. It moves the turtle forward regardless of what the letter is 'F', 'L' and 'R' and THEN does the 'F' 'L' or 'R' operation after moving it forward. I don't want it to move forward every time. Do you have any idea why it does not work the same for me?
– Mandingo
Nov 24 '18 at 5:38
When I use my string = "FLR", I get this: prnt.sc/lmbbho and when I do it the other way I get this output and they're not the same for me: prnt.sc/lmbbj0 - using the string it moves the turtle forward each time before doing the "F", "L" or "R" move and I don't want it to go forward each time. Not sure why it does that. Any ideas?
– Mandingo
Nov 24 '18 at 5:45
1
Nevermind, I fixed it. Thanks for the help anywy.
– Mandingo
Nov 24 '18 at 9:28
1
You're welcome! Glad that I helped somehow. Now I understood that the problem was in the condition if i == "F" or "E", because it'll always be true, and "E" is always true as a condition.
– Luan Naufal
Nov 24 '18 at 12:40
No, that's what I don't want. It moves the turtle forward regardless of what the letter is 'F', 'L' and 'R' and THEN does the 'F' 'L' or 'R' operation after moving it forward. I don't want it to move forward every time. Do you have any idea why it does not work the same for me?
– Mandingo
Nov 24 '18 at 5:38
No, that's what I don't want. It moves the turtle forward regardless of what the letter is 'F', 'L' and 'R' and THEN does the 'F' 'L' or 'R' operation after moving it forward. I don't want it to move forward every time. Do you have any idea why it does not work the same for me?
– Mandingo
Nov 24 '18 at 5:38
When I use my string = "FLR", I get this: prnt.sc/lmbbho and when I do it the other way I get this output and they're not the same for me: prnt.sc/lmbbj0 - using the string it moves the turtle forward each time before doing the "F", "L" or "R" move and I don't want it to go forward each time. Not sure why it does that. Any ideas?
– Mandingo
Nov 24 '18 at 5:45
When I use my string = "FLR", I get this: prnt.sc/lmbbho and when I do it the other way I get this output and they're not the same for me: prnt.sc/lmbbj0 - using the string it moves the turtle forward each time before doing the "F", "L" or "R" move and I don't want it to go forward each time. Not sure why it does that. Any ideas?
– Mandingo
Nov 24 '18 at 5:45
1
1
Nevermind, I fixed it. Thanks for the help anywy.
– Mandingo
Nov 24 '18 at 9:28
Nevermind, I fixed it. Thanks for the help anywy.
– Mandingo
Nov 24 '18 at 9:28
1
1
You're welcome! Glad that I helped somehow. Now I understood that the problem was in the condition if i == "F" or "E", because it'll always be true, and "E" is always true as a condition.
– Luan Naufal
Nov 24 '18 at 12:40
You're welcome! Glad that I helped somehow. Now I understood that the problem was in the condition if i == "F" or "E", because it'll always be true, and "E" is always true as a condition.
– Luan Naufal
Nov 24 '18 at 12:40
add a comment |
I fixed the problem by changing
if i == 'F' or 'E':
to
if i in ['F', 'E']:
and now it works as it should.
add a comment |
I fixed the problem by changing
if i == 'F' or 'E':
to
if i in ['F', 'E']:
and now it works as it should.
add a comment |
I fixed the problem by changing
if i == 'F' or 'E':
to
if i in ['F', 'E']:
and now it works as it should.
I fixed the problem by changing
if i == 'F' or 'E':
to
if i in ['F', 'E']:
and now it works as it should.
answered Nov 24 '18 at 9:29
MandingoMandingo
376212
376212
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451622%2fturtle-graphics-path-using-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
For me your second code works just fine
– Tobias Wilfert
Nov 23 '18 at 18:48
@TobiasWilfert The code runs but it doesn't draw the same thing as the first one, so I assume it's not correct? For me it always moves it forward first before doing a turtle move I want it to.
– Mandingo
Nov 23 '18 at 18:49
Well, it checks ever 'char' in S and the first char is F so it moves forward then turns left and right.
– Tobias Wilfert
Nov 23 '18 at 18:51
please consider adding 2 pictures because for me both codes give exactly the same outcome.
– Tobias Wilfert
Nov 23 '18 at 18:52
@TobiasWilfert This is what I get when I use the string = "FLR": prnt.sc/lmbbho and this is what I get when I do it the other way: prnt.sc/lmbbj0 - When i use the string it always moves it forward first for me and then does the "F" "L" or "L" after moving it forward first, I don't know why and I don't want it to move forward each time. Any ideas why I don't get the same output?
– Mandingo
Nov 24 '18 at 5:43