#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Posts
    1
    Rep Power
    0

    Help with random number game?


    I have only just started learning python about 3 days ago (you can call it 2 because mostly 1 day was trial and error of trying to figure out how to get this program to work) and it's probably something stupid that i can't see or don't know about because i've never done programming before. Anyways it'd be a great help if anyone could point me in the right direction or tell me how to fix this.
    The major three problems are that:
    1. The random number that is generated doesn't seem to be working in the function, no matter what the guess is, it always says it's lower than the 'right number'.
    2. The 'guesses' or number of guesses counter doesn't work, if i put the 'c = 0' outside of the function it gives me a error and if i put it inside then it just resets to 0 everytime i press the 'guess' button.
    3. I can't get the 'Press' function to work, i want it so when you double click something you already put into the list of answers, it'll put it and guess it again without you having to retype it

    and a minor problem is that when i type in 'You have used ',c,'''guess(es).''' on both sides of the ,c, the strings are surrounded by brackets ({}), is there anyway to get rid of these just to make it look more clean?

    well here's the code:
    Code:
    import sys
    from Tkinter import *
    import random
    
    c = 0
    num = random.randint(0,100)
    
    def Guess():
        c = 0
        giveup = 'The answer was ',num,'''XD
    
    '''
        high = '''Nope, Sry try again.
    The number was higher than what you typed.
    
    '''
        low = '''Nope, Sry try again.
    The number was lower than what you typed.
    
    '''
        right = '''Yeap, Good guess :D ;).
    Now do you want to exit?
    You can restart the program for it
    to find anouther random number for you to guess.
    (type "exit" in the entry box to quit)
    
    '''
        guesses = 'You have used',c,'''guess(es).
    
    '''
        text.delete("1.0",END)
        answer = ent.get()
        if answer == num:
            text.insert(END,right)
            yes = 'exit'
            if answer == yes:
                sys.exit()
        if answer > num and answer != 'GU' and answer != 'chances':
            text.insert(END,low)
            c = c + 1
        if answer < num and answer != 'GU' and answer != 'chances':
            text.insert(END,high)
            c = c + 1
        if answer == 'GU':
            text.insert(END,giveup)
        if answer == 'guesses':
            text.insert(END,guesses)
        listb.insert(0,answer)
        ent.delete(0,END)
    
    def Ask():
        text.delete("1.0",END)
        question = range(101)
        question2 = '''
    
    Guess the right number (0-100)
    and fill it in the entry box.
    (Type exit to quit, or 'GU' to give up
    or 'guesses' to see how many guesses
    you have already used to guess the number)
    
    '''
        text.insert(END,question)
        text.insert(END,question2)
    
    def Press():
        text.delete("1.0",END)
        selec = listb.curselection()
        select = listb.get(selec[0])
        ent.insert(0, select)
    
    Window = Tk()
    
    wTitle = Frame(Window)
    wTitle.master.title('Number Guess')
    
    text = Text(Window, bg = 'blue', fg = 'white', width = '50', height = '15')
    ent = Entry(Window, bg = 'white')
    buttona = Button(Window, text = 'Guess', bg = 'blue', fg = 'yellow', width = '10', command = Guess)
    buttonb = Button(Window, text = 'Ask Question', bg = 'red', width = '25', command = Ask)
    label1 = Label(Window, text = 'Guess:')
    label = Label(Window, text = 'Old Answers:')
    listb = Listbox(Window, bg = 'black', fg = 'white', width = '10', height = '7')
    byme = Label(Window, text = 'By: Eclipse')
    
    buttonb.focus()
    
    ent.insert(0, 'Number guess goes here.')
    
    listb.bind("<Double-Button-1>",Press)
    
    buttonb.pack()
    text.pack()
    label1.place(x = 0, y = 230)
    ent.place(x = 40, y = 230)
    buttona.place(x = 0, y = 250)
    label.place(x = 170, y = 230)
    listb.place(x = 237, y = 235)
    byme.pack(pady = 50)
    
    Window.mainloop()
    thanks a bunch and I hope someone can help me.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    ent.get() returns a string. So you compare a string and a number ... that's nonsense Just write
    Code:
    answer = int(ent.get())
    to convert answer to an integer.

    Regards, mawe
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65

    Smile


    For your counter use c that way ...
    Code:
    ...
    c = 0
    num = random.randint(0,100)
    
    def Guess():
        global c
        ...

IMN logo majestic logo threadwatch logo seochat tools logo