2

I created a number guessing game. However, I keep on running into an error.

The problem is when I run it and enter the number it will eventually say the number is higher than 74 (this is an example) but lower than 75... If I set a specific number it works fine, but when I generate a random number it doesn't work.

import random
from tkinter import *

def checkInput():  


   randomNumber = random.randint(1,100)
   user_guess = int(user_input.get())


   if user_guess == randomNumber:
        print("You got it!")
   if user_guess > randomNumber:
        print("Guess lower!")
   if user_guess < randomNumber:
        print("Guess higher!")

root = Tk()

user_input = Entry(root)
user_input.pack()
check_button = Button(root,text="Check",command=checkInput) .pack()
reset_button = Button(root,text="Reset") .pack()

root.mainloop
3
  • 1
    It would help if you post the error log as well Commented Nov 15, 2017 at 22:40
  • 1
    In your real code, do you actually call mainloop? You generate a new random number on each go. So you are guessing a moving target. You should generate the number at the start and compare all subsequent guesses to that number. Commented Nov 15, 2017 at 22:43
  • 1
    I think the problem is that you are generating a random number each time you call to checkInput so previous indications do not apply.... Commented Nov 15, 2017 at 22:44

1 Answer 1

1

the problem is that you are generating the random number in the "checkInput" function which will be called and changed everytime you press the "Check" button. What you should do is to generate the random number once and outside the "checkInput" function, for example look at the following code:

import random
from tkinter import *

def checkInput():  

   user_guess = int(user_input.get())

   if user_guess == randomNumber:
        print("You got it!")
   if user_guess > randomNumber:
        print("Guess lower!")
   if user_guess < randomNumber:
        print("Guess higher!")

randomNumber = random.randint(1,100)
root = Tk()

user_input = Entry(root)
user_input.pack()
check_button = Button(root,text="Check",command=checkInput) .pack()
reset_button = Button(root,text="Reset") .pack()

root.mainloop()

It is basically your own code with a minor change which is that the line where you generate the random number has been now moved out of the "checkInput" function.

Good Luck ...

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much, but I still have one question. You said it would be changed everytime I pressed the "Check" button, but I've only pressed it once.?
@DatOneNoobCoder: It really does not matter how many times you press the "Check" button, it is incorrect to create the random number inside the "checkInput" function. The idea of the game is that the code needs to create a random number first, store it in a variable and keep its value unchanged until the end of program. Meanwhile the user keeps guessing until their guess matches the random number.
@DatOneNoobCoder: The point here is that if you create the random number inside the "checkInput()" a new random number will be created every time this function is called, which is every time the "Check" button is pressed according to your code. You can get lucky and guess a number which matches the random number in the first trial, however the code will not behave as expected if more guesses are required. That's why you see the code tells you that the number is larger than 74 and smaller than 75!!! Let me explain what happens in your code and why you get this strange behaviour.
@DatOneNoobCoder: When you first run your code, "randomNumber" does not have any value because it has not been created yet because you have not pressed the "Check" button yet! Then you put in some number say 74 and press the "Check" button which calls the "checkInput" function. Inside this function "randomNumber" is just created (and this is after you have already made your guess!!!) and say its 80. Now the code compares 80 with 74 and tells you that you need to guess a larger value.
@DatOneNoobCoder: Now you put in 75 and press the "check" button again which calls the "checkInput" function again. Now the "randomNumber" changes and is assigned to a new value say 13, it is no longer 80! Now the code compares 13 with 75 and tells you that you need to put in a smaller number!!! You get confused because how an integer number can be larger than 74 and smaller than 75.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.