I am having a technical problem with a program

from tkinter import *
man = Tk()
man.title(“Hang Man”)
score = 0
def WordPicker():
return “happiness”

CurrentWord = WordPicker()

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

if score < 0:
    score += 0
return score

CurrentScore = Scorer(CurrentWord)

def Hang_man():
if CurrentScore == 0:

elif CurrentScore == 1:
elif CurrentScore == 2:
elif CurrentScore == 3:
elif CurrentScore == 4:
elif CurrentScore == 5:
elif CurrentScore == 6:

background = Canvas(man,bg=“dark grey”,width=600,height=400)
entry = StringVar()
textbox = Entry(man,textvariable=entry,bg=“light grey”)
button = Button(man,text=“PUSH ME!”,command=Hang_man,bg=“dark grey”)
ScoreLabel = Label(man,text="Score: " + str(score))
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)

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.