简体   繁体   中英

Tkinter number guessing game not working

My friend and I are working on a number guessing game for a school project, but no matter what, we can't seem to find the problem with our Tkinter program. The main objective is to have the computer generate a number from 1-100 and the user has to guess the number. We are almost finished for the most part, but whenever we guess the number, the program always returns "go higher," even if the number we guessed is lower than the number the program generates. Please help!!

I have already found a working program that does what we want, but I really want to understand what went wrong in our program

I have inserted our program below:

from Tkinter import *
import random
frame = Tk()

frame.geometry("500x500")
frame.title("guess the number")

global ability
ability = random.randint(1,100)
print ability

def retrieve_input():
    input = t1.get("1.0",'end-1c')
    return input

def startFunction():
    global t2
    frame.destroy()
    frame2 = Tk()

    frame2.geometry("500x247")
    frame2.title("guess the number")

    l1 = Label(text="The Number Guessing Game", font=("Helvetica", 27))
    l1.place(x=10, y=10)

    l2 = Label(text="Guess your number here (0-100)")
    l2.place(x=10, y=100)

    t1 = Text(width= 5, height= 1)
    t1.place(x= 10, y= 124)

    l3 = Label(text="Higher or Lower?")
    l3.place(x=10, y=190)

    t2 = Text(width= 15, height= 1)
    t2.place(x= 10, y= 214)

    b1 = Button(text="Enter", width= 5, height= 1, command= numberFunction)
    b1.place(x= 10, y= 150)

def numberFunction():
    global ability,t2
    if input() == ability:
            t2.insert(END,"correct")
    if input() > ability:
            t2.insert(END,"lower")
    if input() < ability:
            t2.insert(END,"higher")

b1 = Button(text="START", width=12, height=2, command=startFunction)
b1.place(x=210, y=210)

frame.mainloop()

Try using an entry widget:

First, you need to create an Entry Widget:

guess = Entry()
guess.pack()

This will have an entry widget show up. Now, we need to have a callback that takes whatever the user wrote. Add this on to your NumberFunction

user_guess = int(guess.get())

Now, you can compare the user_guess to the random number generated.

if user_guess == ability:
        t2.insert(END,"correct")
if user_guess > ability:
        t2.insert(END,"lower")
if user_guess < ability:
        t2.insert(END,"higher")

Complete code would be:

from tkinter import *
import random
frame = Tk()

frame.geometry("500x500")
frame.title("guess the number")

global ability
ability = random.randint(1,100)
print (ability)

def retrieve_input():
    input = t1.get("1.0",'end-1c')
    return input

def startFunction():
    global t2
    frame.destroy()
    frame2 = Tk()

    frame2.geometry("500x247")
    frame2.title("guess the number")

    l1 = Label(text="The Number Guessing Game", font=("Helvetica", 27))
    l1.place(x=10, y=10)

    l2 = Label(text="Guess your number here (0-100)")
    l2.place(x=10, y=100)

    t1 = Entry(width= 5)
    t1.place(x= 10, y= 124)

    l3 = Label(text="Higher or Lower?")
    l3.place(x=10, y=190)

    t2 = Entry(width= 15)
    t2.place(x= 10, y= 214)

    def numberFunction():
        global ability,t2
        if int(t1.get()) == ability:
            print("Correct")
        if int(t1.get()) > ability:
            print("Guess Lower")
        if int(t1.get()) < ability:
            print("Guess Higher")

    b1 = Button(text="Enter", width= 5, height= 1, command= numberFunction)
    b1.place(x= 10, y= 150)



b1 = Button(text="START", width=12, height=2, command=startFunction)
b1.place(x=210, y=210)

frame.mainloop()

Instead of print guess higher or lower, you can pack or place labels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM