2

I am making a guess the number in Zelle graphics and my program does not seem to be working properly. I am trying to have the Text Entry become an integer. If there are any other problems with what I have done I would appreciate some help.

I have tried to do int(number) but that hasn't worked

from graphics import *

import random

hidden=random.randrange(1,10)

def responseDict():            

    levels = dict()

    levels['high'] = 'woah! you are too high!'

    levels['low']='oh no! that is too low'     

    levels['equal']='yes, this is just right!'

    return levels



def circles():                                                     # cute, but nothing original here, not even usage

    win = GraphWin("Random Circles",300,300)

    for i in range(300):

        r = random.randrange(256)

        b = random.randrange(256)

        g = random.randrange(256)

        color = color_rgb(r, g, b)



        radius = random.randrange(3, 40)

        x = random.randrange(5, 295)          

        y = random.randrange (5, 295)      



        circle = Circle(Point(x,y), radius)

        circle.setFill(color)

        circle.draw(win)

        time.sleep(.05)



def textBox(win):
    message = Text(Point(250,50),'Please guess a number 1 through 10 then click outside the box')
    message.draw(win)

    message2=Text(Point(250,100),'You have 4 tries, to guess the number correctly.')
    message2.draw(win)

    for i in range(9):

        textEntry =Entry(Point(233,200),10)
        textEntry.draw(win)

        win.getMouse()

        number=textEntry.getText()
        guess=int(number)
        print(guess)

        levels = responseDict()

        while guess != hidden:
            if guess < hidden:

                response = Text(Point(300,300), (levels['low']))            
                response.draw(win)


                again=Text(Point(400,400), 'guess again')
                again.draw(win)


                textEntry=Entry(Point(233,200),10)
                textEntry.draw(win)
                win.getMouse()

                number=textEntry.getText()
                guess=int(number)
                print(guess)

                response.undraw()
                again.undraw()
                win.getMouse()
            elif guess > hidden:                                                       

                response2=Text(Point(350,350),(levels['high']))
                response2.draw(win)

                again2=Text(Point(400,400), 'guess again')
                again2.draw(win)

                textEntry2=Entry(Point(233,200),10)
                textEntry2.draw(win)
                win.getMouse()

                number=textEntry.getText()
                guess=int(number)
                print(guess)

                response2.undraw()
                again2.undraw()
                win.getMouse()

            else:
                response=Text(Point(300,300),(levels['equal']))
                response.draw(win)
                win.getMouse()
                circles()



win = GraphWin('guess number', 700,700)                         

win.setBackground('brown')

textBox(win)

exitText = Text(Point(400,400), 'Click anywhere to quit')
exitText.draw(win)

win.getMouse()
win.close()

I want what the user entered to become an integer and my game to work!

furas
  • 134,197
  • 12
  • 106
  • 148
  • your problem is because you don't know how GUI works. `Entry` doesn't works like `input()` - it doesn't wait for user's text and all code after `Entry()` is executed at start, when Entry is empty, so int() try to convert empty string. You may need `Button` which will run code when you put text in Entry and press button. – furas Jul 31 '19 at 17:08
  • @furas how do I add a button? – python student Jul 31 '19 at 17:21
  • now I see you use `getMouse()` which stops code so maybe you don't have to use button. But if you put text in Entry and you try to convert it to integer - `int("Hello")` then you can get your error. You have to use `try/except` to catch this error. – furas Jul 31 '19 at 17:36
  • you don't have to create new Entry in the same place. You can clear existing entry - `textEntry.setText('')` – furas Jul 31 '19 at 17:42

1 Answers1

1

If someone put text instead of number (ie. Hello) then int() gives error

ValueError: invalid literal for int() with base 10: 'Hello'

and you have to use try/except to catch it

    number = textEntry.getText()
    try:
        guess = int(number)
        print(guess)
    except Exception as ex:
        guess = None
        #print(ex)

In except I set guess = None so later I can display message for this

    if guess is None:
        # show message
        response = Text(Point(300, 300), 'It is not number')            
        response.draw(win)

If you not assign value to guess in except then you can get error that this variable doesn't exist - it can happen in first loop when variable wasn't created in previous loop.


My full code (with other changes):

from graphics import *

import random

hidden = random.randrange(1, 10)

def response_dict():            

    return {
        'high': 'woah! you are too high!',
        'low': 'oh no! that is too low',     
        'equal': 'yes, this is just right!',
        'none': 'It is not number',
    }


def circles(): 

    win = GraphWin("Random Circles",300,300)

    for i in range(300):

        r = random.randrange(256)
        b = random.randrange(256)
        g = random.randrange(256)
        color = color_rgb(r, g, b)

        radius = random.randrange(3, 40)
        x = random.randrange(5, 295)          
        y = random.randrange(5, 295)      

        circle = Circle(Point(x, y), radius)
        circle.setFill(color)
        circle.draw(win)

        time.sleep(.05)


def textBox(win):
    message = Text(Point(250,50),'Please guess a number 1 through 10 then click outside the box')
    message.draw(win)

    message2 = Text(Point(250,100),'You have 4 tries, to guess the number correctly.')
    message2.draw(win)

    # you can get it once
    levels = response_dict()

    # 4 tries
    for i in range(4):

        textEntry = Entry(Point(233,200),10) 
        textEntry.draw(win)

        win.getMouse()

        # get number
        number = textEntry.getText()
        try:
            guess = int(number)
            print(guess)
        except Exception as ex:
            #print(ex)
            guess = None

        # hide entry - so user can't put new number 
        textEntry.undraw()

        if guess is None:
            # show message
            response = Text(Point(300,300), levels['none'])            
            response.draw(win)

        elif guess < hidden:
            # show message
            response = Text(Point(300,300), levels['low'])            
            response.draw(win)

        elif guess > hidden:                                                       
            # show message
            response = Text(Point(350, 350), levels['high'])
            response.draw(win)

        else:
            response = Text(Point(300, 300), levels['equal'])
            response.draw(win)
            win.getMouse()
            circles()
            break # exit loop 

        again = Text(Point(400,400), 'Guess again, click mouse.')
        again.draw(win)

        # wait for mouse click
        win.getMouse()

        # remove messages
        response.undraw()
        again.undraw()


# --- main ----

win = GraphWin('guess number', 700, 700)                         
win.setBackground('brown')

textBox(win)

exitText = Text(Point(400, 400), 'Click anywhere to quit')
exitText.draw(win)

win.getMouse()
win.close()
furas
  • 134,197
  • 12
  • 106
  • 148