Unable to record how many times my while loop runs- Python3











up vote
0
down vote

favorite












I am working on a number guessing game for python3 and the end goal of this is to show the user if they play more than one game that they'll receive an average number of guesses. However, I am unable to record how many times the game actually runs. Any help will do.



from random import randint
import sys

def guessinggame():
STOP = '='
a = '>'
b = '<'
guess_count = 0
lowest_number = 1
gamecount = 0
highest_number = 100
while True:
guess = (lowest_number+highest_number)//2
print("My guess is :", guess)
user_guess = input("Is your number greater than,less than, or equal to: ")
guess_count += 1
if user_guess == STOP:
break
if user_guess == a:
lowest_number = guess + 1
elif user_guess == b:
highest_number = guess - 1
print("Congrats on BEATING THE GAME! I did it in ", guess_count, "guesses")
PLAY_AGAIN = input("Would you like to play again? y or n: ")
yes = 'y'
gamecount = 0
no = 'n'
if PLAY_AGAIN == yes:
guessinggame()
gamecount = gamecount + 1
else:
gamecount += 1
print("thank you for playing!")
print("You played", gamecount , "games")
sys.exit(0)
return guess_count, gamecount

print('Hello! What is your name?')
myname = input()

print('Well', myname, ', I want you to think of number in your head and I will guess it.')
print("---------------------------------------------------------------------------------")
print("RULES: if the number is correct simply input '='")
print("---------------------------------------------------------------------------------")
print(" if YOUR number is GREATER then the output, input '>'")
print("---------------------------------------------------------------------------------")
print(" if YOUR number is LESS then the output, input '<'")
print("---------------------------------------------------------------------------------")
print(" ALRIGHT LETS PLAY")
print("---------------------------------------------------------------------------------")



guessinggame()
guess_count = guessinggame()
print(" it took me this many number of guesses: ", guess_count)


## each game the user plays is added one to it
## when the user wants to the game to stop they finish it and
## prints number of games they played as well as the average of guess it took
## it would need to take the number of games and add all the guesses together and divide it.









share|improve this question


























    up vote
    0
    down vote

    favorite












    I am working on a number guessing game for python3 and the end goal of this is to show the user if they play more than one game that they'll receive an average number of guesses. However, I am unable to record how many times the game actually runs. Any help will do.



    from random import randint
    import sys

    def guessinggame():
    STOP = '='
    a = '>'
    b = '<'
    guess_count = 0
    lowest_number = 1
    gamecount = 0
    highest_number = 100
    while True:
    guess = (lowest_number+highest_number)//2
    print("My guess is :", guess)
    user_guess = input("Is your number greater than,less than, or equal to: ")
    guess_count += 1
    if user_guess == STOP:
    break
    if user_guess == a:
    lowest_number = guess + 1
    elif user_guess == b:
    highest_number = guess - 1
    print("Congrats on BEATING THE GAME! I did it in ", guess_count, "guesses")
    PLAY_AGAIN = input("Would you like to play again? y or n: ")
    yes = 'y'
    gamecount = 0
    no = 'n'
    if PLAY_AGAIN == yes:
    guessinggame()
    gamecount = gamecount + 1
    else:
    gamecount += 1
    print("thank you for playing!")
    print("You played", gamecount , "games")
    sys.exit(0)
    return guess_count, gamecount

    print('Hello! What is your name?')
    myname = input()

    print('Well', myname, ', I want you to think of number in your head and I will guess it.')
    print("---------------------------------------------------------------------------------")
    print("RULES: if the number is correct simply input '='")
    print("---------------------------------------------------------------------------------")
    print(" if YOUR number is GREATER then the output, input '>'")
    print("---------------------------------------------------------------------------------")
    print(" if YOUR number is LESS then the output, input '<'")
    print("---------------------------------------------------------------------------------")
    print(" ALRIGHT LETS PLAY")
    print("---------------------------------------------------------------------------------")



    guessinggame()
    guess_count = guessinggame()
    print(" it took me this many number of guesses: ", guess_count)


    ## each game the user plays is added one to it
    ## when the user wants to the game to stop they finish it and
    ## prints number of games they played as well as the average of guess it took
    ## it would need to take the number of games and add all the guesses together and divide it.









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am working on a number guessing game for python3 and the end goal of this is to show the user if they play more than one game that they'll receive an average number of guesses. However, I am unable to record how many times the game actually runs. Any help will do.



      from random import randint
      import sys

      def guessinggame():
      STOP = '='
      a = '>'
      b = '<'
      guess_count = 0
      lowest_number = 1
      gamecount = 0
      highest_number = 100
      while True:
      guess = (lowest_number+highest_number)//2
      print("My guess is :", guess)
      user_guess = input("Is your number greater than,less than, or equal to: ")
      guess_count += 1
      if user_guess == STOP:
      break
      if user_guess == a:
      lowest_number = guess + 1
      elif user_guess == b:
      highest_number = guess - 1
      print("Congrats on BEATING THE GAME! I did it in ", guess_count, "guesses")
      PLAY_AGAIN = input("Would you like to play again? y or n: ")
      yes = 'y'
      gamecount = 0
      no = 'n'
      if PLAY_AGAIN == yes:
      guessinggame()
      gamecount = gamecount + 1
      else:
      gamecount += 1
      print("thank you for playing!")
      print("You played", gamecount , "games")
      sys.exit(0)
      return guess_count, gamecount

      print('Hello! What is your name?')
      myname = input()

      print('Well', myname, ', I want you to think of number in your head and I will guess it.')
      print("---------------------------------------------------------------------------------")
      print("RULES: if the number is correct simply input '='")
      print("---------------------------------------------------------------------------------")
      print(" if YOUR number is GREATER then the output, input '>'")
      print("---------------------------------------------------------------------------------")
      print(" if YOUR number is LESS then the output, input '<'")
      print("---------------------------------------------------------------------------------")
      print(" ALRIGHT LETS PLAY")
      print("---------------------------------------------------------------------------------")



      guessinggame()
      guess_count = guessinggame()
      print(" it took me this many number of guesses: ", guess_count)


      ## each game the user plays is added one to it
      ## when the user wants to the game to stop they finish it and
      ## prints number of games they played as well as the average of guess it took
      ## it would need to take the number of games and add all the guesses together and divide it.









      share|improve this question













      I am working on a number guessing game for python3 and the end goal of this is to show the user if they play more than one game that they'll receive an average number of guesses. However, I am unable to record how many times the game actually runs. Any help will do.



      from random import randint
      import sys

      def guessinggame():
      STOP = '='
      a = '>'
      b = '<'
      guess_count = 0
      lowest_number = 1
      gamecount = 0
      highest_number = 100
      while True:
      guess = (lowest_number+highest_number)//2
      print("My guess is :", guess)
      user_guess = input("Is your number greater than,less than, or equal to: ")
      guess_count += 1
      if user_guess == STOP:
      break
      if user_guess == a:
      lowest_number = guess + 1
      elif user_guess == b:
      highest_number = guess - 1
      print("Congrats on BEATING THE GAME! I did it in ", guess_count, "guesses")
      PLAY_AGAIN = input("Would you like to play again? y or n: ")
      yes = 'y'
      gamecount = 0
      no = 'n'
      if PLAY_AGAIN == yes:
      guessinggame()
      gamecount = gamecount + 1
      else:
      gamecount += 1
      print("thank you for playing!")
      print("You played", gamecount , "games")
      sys.exit(0)
      return guess_count, gamecount

      print('Hello! What is your name?')
      myname = input()

      print('Well', myname, ', I want you to think of number in your head and I will guess it.')
      print("---------------------------------------------------------------------------------")
      print("RULES: if the number is correct simply input '='")
      print("---------------------------------------------------------------------------------")
      print(" if YOUR number is GREATER then the output, input '>'")
      print("---------------------------------------------------------------------------------")
      print(" if YOUR number is LESS then the output, input '<'")
      print("---------------------------------------------------------------------------------")
      print(" ALRIGHT LETS PLAY")
      print("---------------------------------------------------------------------------------")



      guessinggame()
      guess_count = guessinggame()
      print(" it took me this many number of guesses: ", guess_count)


      ## each game the user plays is added one to it
      ## when the user wants to the game to stop they finish it and
      ## prints number of games they played as well as the average of guess it took
      ## it would need to take the number of games and add all the guesses together and divide it.






      python-3.x if-statement while-loop binary numbers






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 18:31









      Dom L

      235




      235
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          It is because you are either calling guessinggame() everytime user wants to play again or you are exiting the program. Also you are setting gamecount to 0 every time you call guessinggame(). You should move gamecount declaration and initialization out of your function. Also increment gamecount before you call guessinggame().






          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%2f53231487%2funable-to-record-how-many-times-my-while-loop-runs-python3%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



            accepted










            It is because you are either calling guessinggame() everytime user wants to play again or you are exiting the program. Also you are setting gamecount to 0 every time you call guessinggame(). You should move gamecount declaration and initialization out of your function. Also increment gamecount before you call guessinggame().






            share|improve this answer

























              up vote
              1
              down vote



              accepted










              It is because you are either calling guessinggame() everytime user wants to play again or you are exiting the program. Also you are setting gamecount to 0 every time you call guessinggame(). You should move gamecount declaration and initialization out of your function. Also increment gamecount before you call guessinggame().






              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                It is because you are either calling guessinggame() everytime user wants to play again or you are exiting the program. Also you are setting gamecount to 0 every time you call guessinggame(). You should move gamecount declaration and initialization out of your function. Also increment gamecount before you call guessinggame().






                share|improve this answer












                It is because you are either calling guessinggame() everytime user wants to play again or you are exiting the program. Also you are setting gamecount to 0 every time you call guessinggame(). You should move gamecount declaration and initialization out of your function. Also increment gamecount before you call guessinggame().







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 18:48









                Martin Kumecký

                685




                685






























                    draft saved

                    draft discarded




















































                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


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




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231487%2funable-to-record-how-many-times-my-while-loop-runs-python3%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

                    Landwehr

                    Reims

                    Schenkenzell