0

After I clicked on the 'BEGIN' button, the code produced a ValueError:

File "E:\python\number guess trial2.py", line 58, in clicked4

run_game()

File "E:\python\number guess trial2.py", line 61, in run_game

guess = int(guess_entry.get())

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

May someone please explain o me why it produced a ValueError?

#Import tkinter module and random from tkinter module
from tkinter import *
import random
import time

win = Tk()
win.configure(cursor='gumby', bg='yellow')
win.title('A sImPlE gUeSsInG gAmE')
win.wm_iconbitmap('favicon.ico')
number = random.randint(1, 101) #set number as a random integer
f = Frame(win)
#No play button(NO)
def clicked():
    win.destroy()

#Play button (YES)


def clicked1():
    #Erase previous screen
    l.destroy()
    l2.destroy()
    NO.destroy()
    YES.destroy()

    win.title('Are you READY?')
    win.wm_iconbitmap('favicon.ico')
    win.configure(background = "deep sky blue", cursor='rtl_logo')
    f2 = Frame(win)    
    l3.grid(row = 1, column = 4, columnspan=5)

    #'Next' button
    NEXT.grid(row = 5, column = 6)

#NEXT button command
def clicked2():
    win.title('Are you READY?')
    win.wm_iconbitmap('favicon.ico')
    win.configure(background = "deep sky blue", cursor='rtl_logo')
    f3 = Frame(win)
    l3.destroy()
    NEXT.destroy()   
    l4.grid(row = 1, column = 3, columnspan=5)    
    NEXT2.grid(row = 4, column = 5)

#Ready to begin screen
def clicked3():
    win.title('READY?')
    l4.destroy()
    NEXT2.destroy()
    l5.grid(row = 1, column = 3, columnspan=6)
    BEGIN.grid(row = 3, column = 6)

#START button's command
def clicked4():
    l5.destroy()
    BEGIN.destroy()
    run_game()
    #Submit button's command
def run_game():
    guess = int(guess_entry.get())
    if guess != number:
             print_text = "you guessed {0}.".format(guess)

    if guess > number:
            print_text = ("That's too high. Guess lower...")
    elif guess < number:
            print_text = ("That's too low. Guess higher...")

            win.text.delete(0.0, END)
            win.text.insert(0.0, print_text)

            guess_entry.delete(0, END)
    else:
           print_text = ("That's the right number! Well done!")
           win.text.delete(0.0, END)
           win.text.insert(0.0, print_text)
           start_again()

    L2.grid(row=1, column=1)

    #Guess box label
    L1.grid(row=2, column=1)

    #Guess box / add entry box for typing guess
    guess_entry.grid(row=2, column=4)

    #Submit button
    submit.grid(row = 3, column = 4)


    # create computer feedback text box
    win.text.grid(row = 20, column = 0, columnspan = 5)

#'START AGAIN?' SCREEN
def start_again():
    L2.destroy()
    L1.destroy()
    guess_entry.destroy()
    submit.destroy()
    win.text.destroy()
    AGAIN = Label(win, text = 'Start Again?', bg = 'deep sky blue', fg = 'yellow', font=('Snap ITC', 30)) 

#Intro
l = Label(win, text = "Welcome to a number game child.", font=('Snap ITC', 30), bg='yellow', fg='slateblue')
l2 = Label(win, text = "Would you like to play?", font=('Snap ITC', 30), bg = 'yellow', fg='slateblue')
l.grid(row = 1, column = 3, columnspan=5)
l2.grid(row = 2, column = 3, columnspan=5)

#Play or not buttons(YES/NO)
NO = Button(win, text = 'NO', command=clicked, relief=RAISED, padx=30, pady=1 )
NO.config(bg='DodgerBlue2', fg='spring green', font=('Snap ITC', 10), bd=6)
NO.grid(row = 4, column = 4)
YES = Button(win, text = 'YES', command=clicked1, relief=RAISED, padx=30, pady=1)
YES.config(bg='DodgerBlue2', fg='spring green', font=('Snap ITC', 10), bd=6)
YES.grid(row = 4, column = 6)

#SCREEN 2
l3 = Label(win, text = 'The rule is simple. You have 5 chances to \n guess what number I am thinking of.', bg = 'deep sky blue', fg = 'yellow', font=('Snap ITC', 30))
NEXT = Button(win, text = 'NEXT', command=clicked2, bd=6,relief=RAISED, bg = 'firebrick1', fg='DarkOrchid1', padx=30, pady=1, font=('Snap ITC', 10))

#SCREEN 3
l4 = Label(win, text = 'I am thinking of a number between 1 to 100.\n Good Luck!', bg = 'deep sky blue', fg = 'yellow', font=('Snap ITC', 20))
NEXT2 = Button(win, text ='NEXT', command=clicked3, bd=6, relief=RAISED, bg = 'firebrick1', fg='DarkOrchid1', padx=30, pady=1, font=('Snap ITC', 10 ))

#SCREEN 4
l5 = Label(win, text = "Are You Ready?", bg = 'deep sky blue', fg = 'yellow', font=('Snap ITC', 30))
BEGIN = Button(win, text = 'BEGIN', command=clicked4, bd=6, relief=RAISED, bg = 'firebrick1', fg='DarkOrchid1', padx=30, pady=1, font=('Snap ITC', 10))

#SCREEN 5
L2 = Label(win, text = 'A Number Guessing Game', bg='yellow', fg='slateblue', underline=1, font=('Papyrus', 18))
L1 = Label(win, text = 'Make your guess here ----->', bg='yellow', fg='slateblue', font=('Papyrus', 18))
guess_entry = Entry(win, bg = 'turquoise', fg = 'magenta2', cursor = 'pencil')
submit = Button(win, text = "OK", command = run_game, bg = 'purple', fg = 'yellow')
win.text = Text(win, width = 35, height = 1, bg='slateblue', fg='springgreen2')
  • Possible duplicate of [ValueError: invalid literal for int() with base 10: '' in python code](https://stackoverflow.com/questions/43485393/valueerror-invalid-literal-for-int-with-base-10-in-python-code) – mij Jul 31 '18 at 06:25

3 Answers3

1

Did you try to debug the code? Try printing guess_entry.get() and see if the value is integer. Probably, the compiler is not to parse the value to integer. You can use try-except to handle the ValueError exception.

Pranali Rasal
  • 177
  • 2
  • 12
1

You call guess_entry.get() before you give the user the chance to type anything in the entry box. Therefore, the entry box is empty so guess_entry.get() returns an empty string, which can't be converted to an integer.

The easiest thing to do is to split up the run_game function into a function that sets up the game for the first time and a function that processes each guess. I also fixed some indentation errors in your run_game function and identified two other issues, which I've commented. Please note that this will still give the same ValueError if the user types in anything that cannot be converted to an integer. You could solve that using a try-catch block or using entry validation.

def clicked4():
    l5.destroy()
    BEGIN.destroy()
    init_game()

def init_game():
    L2.grid(row=1, column=1)
    #Guess box label
    L1.grid(row=2, column=1)
    #Guess box / add entry box for typing guess
    guess_entry.grid(row=2, column=4)
    #Submit button
    submit.grid(row = 3, column = 4)
    # create computer feedback text box
    win.text.grid(row = 20, column = 0, columnspan = 5)

def run_game():
    guess = int(guess_entry.get())
    if guess != number:
        print_text = "you guessed {0}.".format(guess) # This does nothing, you always overwrite it.

        if guess > number:
            print_text = ("That's too high. Guess lower...")
        elif guess < number:
            print_text = ("That's too low. Guess higher...")

        win.text.delete(0.0, END)
        win.text.insert(0.0, print_text)
        guess_entry.delete(0, END)
    else:
       print_text = ("That's the right number! Well done!")
       win.text.delete(0.0, END)
       win.text.insert(0.0, print_text)
       start_again() # This fires immediately, so you will never see the message
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
0

I inserted this line print "--->'", guess_entry.get(), "'" just before your line of guess = int(guess_entry.get())

And I get --->' '

So it would seem that your guess_entry.get() has a value of spaces which would explain your error.

Chai Ang
  • 474
  • 1
  • 6
  • 11