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.










share|improve this question
























  • "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















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.










share|improve this question
























  • "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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 executing current_scene.enter() every iteration of the while 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










  • 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
















"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












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.






share|improve this answer





















    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',
    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
    });


    }
    });














     

    draft saved


    draft discarded


















    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
























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 23:33









        kpie

        3,38741431




        3,38741431






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff