I am having a technical problem with a program

from tkinter import *
man = Tk()
#---------------------
man.geometry(“650x500”)
man.title(“Hang Man”)
man.configure(bg=“grey”)
#---------------------
score = 0
#Functions-------------
def WordPicker():
return “happiness”

CurrentWord = WordPicker()

def Scorer(Word):
if len(entry.get()) == 1:
if entry.get() in word:
score += 1
else:
score -=1
else:
score -= 1

if score < 0:
    abs(score)
else:
    score += 0
return score

CurrentScore = Scorer(CurrentWord)

def Hang_man():
if CurrentScore == 0:
TheHangMan.config(image=hang7)

elif CurrentScore == 1:
    TheHangMan.config(image=hang6)
elif CurrentScore == 2:
    TheHangMan.config(image=hang5)
elif CurrentScore == 3:
    TheHangMan.config(image=hang4)
elif CurrentScore == 4:
    TheHangMan.config(image=hang3)
elif CurrentScore == 5:
    TheHangMan.config(image=hang2)
elif CurrentScore == 6:
    TheHangMan.config(image=hang1)

#Canvas---------------
background = Canvas(man,bg=“dark grey”,width=600,height=400)
background.place(x=0,y=0)
#Entry----------------
entry = StringVar()
textbox = Entry(man,textvariable=entry,bg=“light grey”)
textbox.place(x=10,y=410)
#---------------------
#Button---------------
button = Button(man,text=“PUSH ME!”,command=Hang_man,bg=“dark grey”)
button.place(x=10,y=450)
#---------------------
#Labels---------------
ScoreLabel = Label(man,text="Score: " + str(score))
ScoreLabel.place(x=650,y=100)
#---------------------
#Image----------------
hang1 = PhotoImage(file=“h1.gif”)
hang2 = PhotoImage(file=“h2.gif”)
hang3 = PhotoImage(file=“h3.gif”)
hang4 = PhotoImage(file=“h4.gif”)
hang6 = PhotoImage(file=“h5.gif”)
hang7 = PhotoImage(file=“h6.gif”)
hang7 = PhotoImage(file=“h7.gif”)
global TheHangMan
TheHangMan = Label(man,image=hang1)
TheHangMan.place(x=0,y=0)
#------------------------

Traceback (most recent call last):
File “C:\Users\hatim\Desktop\files\programs\python programs\hang man\Hang_Man.py”, line 30, in
CurrentScore = Scorer(CurrentWord)
File “C:\Users\hatim\Desktop\files\programs\python programs\hang man\Hang_Man.py”, line 16, in Scorer
if len(entry.get()) == 1:
NameError: name ‘entry’ is not defined

What is the problem with the program?

In order to preserve formatting, please select any code or traceback that you post and then click the </> button.

This line:

CurrentScore = Scorer(CurrentWord)

calls Scorer, which runs this line:

    if len(entry.get()) == 1:

However, entry hasn’t been defined yet.

In fact, entry is only defined much later, here:

entry = StringVar()

but it never gets that far because of the exception.

Also, make sure that you convert the entry to type int. As the function clearly implies, it is a str string type variable (StringVar()). So, convert it by prefixing

if len(entry.get()) == 1:

like this:

if len(int(entry.get())) == 1:

since you’re making a comparison with an integer value. You can’t compare string types with integer types (almost a literal example of cannot compare apples to oranges).

One small suggestion, replace this:

from tkinter import *

with this:

import tkinter as tk

Then prefix all of the functions that you’re using from the tkinter package module with tk. This serves two purposes at least:

  1. Keeps tkinter names from colliding with your native script names.
  2. It makes it easy to spot between your native names to those from the tkinter package module.

This helps in better understanding your script both for you and for any person that is reviewing (debugging) it.

I didn’t have to define the entry box variable before in a different program when the variable was define later on in the program.

Also, Thank you.

Thank you both of you.

The line:

if len(entry.get()) == 1:

is checking the length, and that is an integer.

You’re right. I actually overlooked the len keyword prefix.

In my mind, I was thinking in terms of (wasn’t on my full senses obviously):

if entry.get() == 1:

where the value is dependent on what the user types in the entry box which is then represented by the tkinter variable entry. It almost doesn’t make sense to take the length of a string entered. Generally its a value that is entered.

True, but if it was the value that was wanted, trying to use len on it wouldn’t make any sense! :slight_smile:

Exacto mundo. Sorry, its late over here. I am in zombie mode. If mistakes are made when conditions are ideal and normal, what can one expect when the system is about to shut down. :sleeping:

Thank you both very much.