While loop indentation is messing with my variables
up vote
0
down vote
favorite
I'm very new to programming and I have a piece of code that works one way, but only works semi-correctly in two other ways. I simply want to understand why.
This is from a text game that I am following:
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
this piece of code works correctly, however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop, as shown below
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
In other parts of my code, the scenes contain input. When this line is indented to be part of the while loop, what happens is when you give the correct input, it goes through the same scene again. Giving it the correct input a second time finally allows it to continue with the rest of the game. Why does this happen?
Lastly, if I move current_scene.enter() above the while loop, it repeats the first scene twice after the correct input, why is that?
below is what it looks like
current_scene.enter()
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
I appreciate any and all help. This is my first question on StackOverflow and I apologize if I did this incorrectly.
python while-loop indentation
add a comment |
up vote
0
down vote
favorite
I'm very new to programming and I have a piece of code that works one way, but only works semi-correctly in two other ways. I simply want to understand why.
This is from a text game that I am following:
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
this piece of code works correctly, however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop, as shown below
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
In other parts of my code, the scenes contain input. When this line is indented to be part of the while loop, what happens is when you give the correct input, it goes through the same scene again. Giving it the correct input a second time finally allows it to continue with the rest of the game. Why does this happen?
Lastly, if I move current_scene.enter() above the while loop, it repeats the first scene twice after the correct input, why is that?
below is what it looks like
current_scene.enter()
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
I appreciate any and all help. This is my first question on StackOverflow and I apologize if I did this incorrectly.
python while-loop indentation
"however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop" - it would make it part of the while loop, but appropriately? No. That call is not supposed to be inside the loop.
– user2357112
Nov 8 at 23:31
In the second one, you're executingcurrent_scene.enter()
every iteration of thewhile
loop. Not sure what your desired behavior is.
– Tomothy32
Nov 8 at 23:32
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm very new to programming and I have a piece of code that works one way, but only works semi-correctly in two other ways. I simply want to understand why.
This is from a text game that I am following:
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
this piece of code works correctly, however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop, as shown below
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
In other parts of my code, the scenes contain input. When this line is indented to be part of the while loop, what happens is when you give the correct input, it goes through the same scene again. Giving it the correct input a second time finally allows it to continue with the rest of the game. Why does this happen?
Lastly, if I move current_scene.enter() above the while loop, it repeats the first scene twice after the correct input, why is that?
below is what it looks like
current_scene.enter()
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
I appreciate any and all help. This is my first question on StackOverflow and I apologize if I did this incorrectly.
python while-loop indentation
I'm very new to programming and I have a piece of code that works one way, but only works semi-correctly in two other ways. I simply want to understand why.
This is from a text game that I am following:
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
this piece of code works correctly, however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop, as shown below
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
current_scene.enter()
In other parts of my code, the scenes contain input. When this line is indented to be part of the while loop, what happens is when you give the correct input, it goes through the same scene again. Giving it the correct input a second time finally allows it to continue with the rest of the game. Why does this happen?
Lastly, if I move current_scene.enter() above the while loop, it repeats the first scene twice after the correct input, why is that?
below is what it looks like
current_scene.enter()
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
I appreciate any and all help. This is my first question on StackOverflow and I apologize if I did this incorrectly.
python while-loop indentation
python while-loop indentation
edited Nov 8 at 23:45
martineau
64.6k887172
64.6k887172
asked Nov 8 at 23:28
user10626377
"however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop" - it would make it part of the while loop, but appropriately? No. That call is not supposed to be inside the loop.
– user2357112
Nov 8 at 23:31
In the second one, you're executingcurrent_scene.enter()
every iteration of thewhile
loop. Not sure what your desired behavior is.
– Tomothy32
Nov 8 at 23:32
add a comment |
"however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop" - it would make it part of the while loop, but appropriately? No. That call is not supposed to be inside the loop.
– user2357112
Nov 8 at 23:31
In the second one, you're executingcurrent_scene.enter()
every iteration of thewhile
loop. Not sure what your desired behavior is.
– Tomothy32
Nov 8 at 23:32
"however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop" - it would make it part of the while loop, but appropriately? No. That call is not supposed to be inside the loop.
– user2357112
Nov 8 at 23:31
"however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop" - it would make it part of the while loop, but appropriately? No. That call is not supposed to be inside the loop.
– user2357112
Nov 8 at 23:31
In the second one, you're executing
current_scene.enter()
every iteration of the while
loop. Not sure what your desired behavior is.– Tomothy32
Nov 8 at 23:32
In the second one, you're executing
current_scene.enter()
every iteration of the while
loop. Not sure what your desired behavior is.– Tomothy32
Nov 8 at 23:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
It looks like current_scene.enter() returns the next scene name and you need to call current_scene.enter() on the final scene to roll credits.
while current_scene != last_scene:
current_scene = self.scene_map.next_scene(current_scene.enter())
current_scene.enter()
perhaps the logic makes sense without the extra variable. Although the "extra variable" might be necessary because scene_map.next_scene(...)
might not return the next scene name like current_scene.enter()
does.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
It looks like current_scene.enter() returns the next scene name and you need to call current_scene.enter() on the final scene to roll credits.
while current_scene != last_scene:
current_scene = self.scene_map.next_scene(current_scene.enter())
current_scene.enter()
perhaps the logic makes sense without the extra variable. Although the "extra variable" might be necessary because scene_map.next_scene(...)
might not return the next scene name like current_scene.enter()
does.
add a comment |
up vote
1
down vote
It looks like current_scene.enter() returns the next scene name and you need to call current_scene.enter() on the final scene to roll credits.
while current_scene != last_scene:
current_scene = self.scene_map.next_scene(current_scene.enter())
current_scene.enter()
perhaps the logic makes sense without the extra variable. Although the "extra variable" might be necessary because scene_map.next_scene(...)
might not return the next scene name like current_scene.enter()
does.
add a comment |
up vote
1
down vote
up vote
1
down vote
It looks like current_scene.enter() returns the next scene name and you need to call current_scene.enter() on the final scene to roll credits.
while current_scene != last_scene:
current_scene = self.scene_map.next_scene(current_scene.enter())
current_scene.enter()
perhaps the logic makes sense without the extra variable. Although the "extra variable" might be necessary because scene_map.next_scene(...)
might not return the next scene name like current_scene.enter()
does.
It looks like current_scene.enter() returns the next scene name and you need to call current_scene.enter() on the final scene to roll credits.
while current_scene != last_scene:
current_scene = self.scene_map.next_scene(current_scene.enter())
current_scene.enter()
perhaps the logic makes sense without the extra variable. Although the "extra variable" might be necessary because scene_map.next_scene(...)
might not return the next scene name like current_scene.enter()
does.
answered Nov 8 at 23:33
kpie
3,38741431
3,38741431
add a comment |
add a comment |
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%2f53217695%2fwhile-loop-indentation-is-messing-with-my-variables%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
"however, I thought that indenting current_scene.enter() would appropriately make it part of the while loop" - it would make it part of the while loop, but appropriately? No. That call is not supposed to be inside the loop.
– user2357112
Nov 8 at 23:31
In the second one, you're executing
current_scene.enter()
every iteration of thewhile
loop. Not sure what your desired behavior is.– Tomothy32
Nov 8 at 23:32