Variable is not used












2















this would be my first question up here so don't be mad if i do something wrong. I got this homework of guessing the secret number and i had to implement Tkinter in it. The problem is that the counter isn't working since the variable counter is not used. I get it, but i tough i code it as it should be, and it looks like not. And for another question, bellow else statement i used IF again, is this OK or do you practice different code.



    import Tkinter
import random
import tkMessageBox

secret_number = random.randint(1, 10)

window = Tkinter.Tk()

greeting = Tkinter.Label(window, text="Welcome to the game " + "guess the secret number".upper() + ". You have 5 tries.n Select a number between 1 and 10:")
greeting.pack()


guess = Tkinter.Entry(window)
guess.pack()


def check_guess():
counter = 0
if int(guess.get()) == secret_number:
counter = counter + 1 #here it states "variable not used"
result_text = "You guessed correctly! Secret number is %s! " % secret_number
elif int(guess.get()) > secret_number:
counter = counter + 1
result_text = "Wrong. Your number is to HIGH! This was your %s try." % counter
else:
counter = counter + 1
result_text = "Wrong. Your number is to LOW! This was your %s try." % counter
if counter == 5 and guess != secret_number:
print "Secret number was %s!" % secret_number

tkMessageBox.showinfo("Result", result_text)


submit = Tkinter.Button(window, text="Try your luck", command=check_guess) # check_guess, not check_guess()
submit.pack()

window.mainloop()









share|improve this question





























    2















    this would be my first question up here so don't be mad if i do something wrong. I got this homework of guessing the secret number and i had to implement Tkinter in it. The problem is that the counter isn't working since the variable counter is not used. I get it, but i tough i code it as it should be, and it looks like not. And for another question, bellow else statement i used IF again, is this OK or do you practice different code.



        import Tkinter
    import random
    import tkMessageBox

    secret_number = random.randint(1, 10)

    window = Tkinter.Tk()

    greeting = Tkinter.Label(window, text="Welcome to the game " + "guess the secret number".upper() + ". You have 5 tries.n Select a number between 1 and 10:")
    greeting.pack()


    guess = Tkinter.Entry(window)
    guess.pack()


    def check_guess():
    counter = 0
    if int(guess.get()) == secret_number:
    counter = counter + 1 #here it states "variable not used"
    result_text = "You guessed correctly! Secret number is %s! " % secret_number
    elif int(guess.get()) > secret_number:
    counter = counter + 1
    result_text = "Wrong. Your number is to HIGH! This was your %s try." % counter
    else:
    counter = counter + 1
    result_text = "Wrong. Your number is to LOW! This was your %s try." % counter
    if counter == 5 and guess != secret_number:
    print "Secret number was %s!" % secret_number

    tkMessageBox.showinfo("Result", result_text)


    submit = Tkinter.Button(window, text="Try your luck", command=check_guess) # check_guess, not check_guess()
    submit.pack()

    window.mainloop()









    share|improve this question



























      2












      2








      2








      this would be my first question up here so don't be mad if i do something wrong. I got this homework of guessing the secret number and i had to implement Tkinter in it. The problem is that the counter isn't working since the variable counter is not used. I get it, but i tough i code it as it should be, and it looks like not. And for another question, bellow else statement i used IF again, is this OK or do you practice different code.



          import Tkinter
      import random
      import tkMessageBox

      secret_number = random.randint(1, 10)

      window = Tkinter.Tk()

      greeting = Tkinter.Label(window, text="Welcome to the game " + "guess the secret number".upper() + ". You have 5 tries.n Select a number between 1 and 10:")
      greeting.pack()


      guess = Tkinter.Entry(window)
      guess.pack()


      def check_guess():
      counter = 0
      if int(guess.get()) == secret_number:
      counter = counter + 1 #here it states "variable not used"
      result_text = "You guessed correctly! Secret number is %s! " % secret_number
      elif int(guess.get()) > secret_number:
      counter = counter + 1
      result_text = "Wrong. Your number is to HIGH! This was your %s try." % counter
      else:
      counter = counter + 1
      result_text = "Wrong. Your number is to LOW! This was your %s try." % counter
      if counter == 5 and guess != secret_number:
      print "Secret number was %s!" % secret_number

      tkMessageBox.showinfo("Result", result_text)


      submit = Tkinter.Button(window, text="Try your luck", command=check_guess) # check_guess, not check_guess()
      submit.pack()

      window.mainloop()









      share|improve this question
















      this would be my first question up here so don't be mad if i do something wrong. I got this homework of guessing the secret number and i had to implement Tkinter in it. The problem is that the counter isn't working since the variable counter is not used. I get it, but i tough i code it as it should be, and it looks like not. And for another question, bellow else statement i used IF again, is this OK or do you practice different code.



          import Tkinter
      import random
      import tkMessageBox

      secret_number = random.randint(1, 10)

      window = Tkinter.Tk()

      greeting = Tkinter.Label(window, text="Welcome to the game " + "guess the secret number".upper() + ". You have 5 tries.n Select a number between 1 and 10:")
      greeting.pack()


      guess = Tkinter.Entry(window)
      guess.pack()


      def check_guess():
      counter = 0
      if int(guess.get()) == secret_number:
      counter = counter + 1 #here it states "variable not used"
      result_text = "You guessed correctly! Secret number is %s! " % secret_number
      elif int(guess.get()) > secret_number:
      counter = counter + 1
      result_text = "Wrong. Your number is to HIGH! This was your %s try." % counter
      else:
      counter = counter + 1
      result_text = "Wrong. Your number is to LOW! This was your %s try." % counter
      if counter == 5 and guess != secret_number:
      print "Secret number was %s!" % secret_number

      tkMessageBox.showinfo("Result", result_text)


      submit = Tkinter.Button(window, text="Try your luck", command=check_guess) # check_guess, not check_guess()
      submit.pack()

      window.mainloop()






      python tkinter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 20:50









      petezurich

      3,52581734




      3,52581734










      asked Nov 24 '18 at 20:37









      NeuroticoNeurotico

      133




      133
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Everytime the function is called, counter will be set to 0, meaning it will never be equal to 5. A simple fix could be to use a global variable instead. Set counter = 0 outside the function instead, and remove that line from the function. Instead, write global counter at the first line in the function. Something like this:



          counter = 0
          def check_guess():
          global counter
          if int(guess.get()) == secret_number:
          ...





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


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462165%2fvariable-is-not-used%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









            1














            Everytime the function is called, counter will be set to 0, meaning it will never be equal to 5. A simple fix could be to use a global variable instead. Set counter = 0 outside the function instead, and remove that line from the function. Instead, write global counter at the first line in the function. Something like this:



            counter = 0
            def check_guess():
            global counter
            if int(guess.get()) == secret_number:
            ...





            share|improve this answer




























              1














              Everytime the function is called, counter will be set to 0, meaning it will never be equal to 5. A simple fix could be to use a global variable instead. Set counter = 0 outside the function instead, and remove that line from the function. Instead, write global counter at the first line in the function. Something like this:



              counter = 0
              def check_guess():
              global counter
              if int(guess.get()) == secret_number:
              ...





              share|improve this answer


























                1












                1








                1







                Everytime the function is called, counter will be set to 0, meaning it will never be equal to 5. A simple fix could be to use a global variable instead. Set counter = 0 outside the function instead, and remove that line from the function. Instead, write global counter at the first line in the function. Something like this:



                counter = 0
                def check_guess():
                global counter
                if int(guess.get()) == secret_number:
                ...





                share|improve this answer













                Everytime the function is called, counter will be set to 0, meaning it will never be equal to 5. A simple fix could be to use a global variable instead. Set counter = 0 outside the function instead, and remove that line from the function. Instead, write global counter at the first line in the function. Something like this:



                counter = 0
                def check_guess():
                global counter
                if int(guess.get()) == secret_number:
                ...






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 20:45









                Ted Klein BergmanTed Klein Bergman

                3,38421328




                3,38421328






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462165%2fvariable-is-not-used%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

                    Lallio

                    Futebolista

                    Jornalista