# I am having a technical problem with a program

**URL:** <https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867>\
**Category:** Python Help\
**Tags:** help\
**Created:** [July 11, 2024, 12:56am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867 "2024-07-11T00:56:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hatimaldos926](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hatimaldos926](https://discuss.python.org/u/hatimaldos926)\
**Post date:** [July 11, 2024, 12:56am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/1 "2024-07-11T00:56:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 11, 2024, 1:27am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/2 "2024-07-11T01:27:18Z")

</div>

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

This line:

```python
CurrentScore = Scorer(CurrentWord)

```

calls `Scorer`, which runs this line:

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

```

However, `entry` hasn’t been defined yet.

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

```python
entry = StringVar()

```

but it never gets that far because of the exception.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [July 11, 2024, 1:45am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/3 "2024-07-11T01:45:28Z")

</div>

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

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

```

like this:

```python
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:

```python
from tkinter import *

```

with this:

```python
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.

---

<div class="post-metadata">

**Author:** ![hatimaldos926](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hatimaldos926](https://discuss.python.org/u/hatimaldos926)\
**Post date:** [July 11, 2024, 2:01am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/4 "2024-07-11T02:01:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hatimaldos926](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hatimaldos926](https://discuss.python.org/u/hatimaldos926)\
**Post date:** [July 11, 2024, 2:02am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/5 "2024-07-11T02:02:58Z")

</div>

Also, Thank you.

---

<div class="post-metadata">

**Author:** ![hatimaldos926](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hatimaldos926](https://discuss.python.org/u/hatimaldos926)\
**Post date:** [July 11, 2024, 2:04am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/6 "2024-07-11T02:04:04Z")

</div>

Thank you both of you.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 11, 2024, 2:59am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/7 "2024-07-11T02:59:36Z")

</div>

The line:

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

```

is checking the _length_, and that is an integer.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [July 11, 2024, 3:16am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/8 "2024-07-11T03:16:58Z")

</div>

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):

```python
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.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 11, 2024, 3:22am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/9 "2024-07-11T03:22:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [July 11, 2024, 3:28am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/10 "2024-07-11T03:28:56Z")

</div>

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. 😴

---

<div class="post-metadata">

**Author:** ![hatimaldos926](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hatimaldos926](https://discuss.python.org/u/hatimaldos926)\
**Post date:** [July 12, 2024, 12:00am UTC](https://discuss.python.org/t/i-am-having-a-technical-problem-with-a-program/57867/11 "2024-07-12T00:00:01Z")

</div>

Thank you both very much.
