1
votes

I am currently coding a hangman game using python and tkinter. However the code does not work. I keep getting the 'in <string>' requires string as left operand, not NoneType problem and i suspect that is the wole reason for the hrat_hru() function not working. If anyone finds any mistake in my code and corrects me, I will be very glad too. Here is my code that is somehow relevant:

from tkinter import*
from tkinter import messagebox
from tkinter import ttk
import random

main = Tk()

#getting the number of tries from an user
def pokusy_hodnota(pokusy_combobox):
    pokusy_p = int(pokusy_combobox.get())
    #messagebox.showinfo("Vybraný počet pokusů", pokusy_p)
    return pokusy_p

zadane = ''
entry = Text(zadane_rm, width=43, height=3)
entry.grid(row=1, columnspan=40)

#getting the entered key from an user
def select(value):
    global zadane
    if value=='Space':
        entry.insert('end', '')
    else:
        entry.insert('end', value)
        zadane = value
        print(f'{zadane=!r}')

#choosing a word that will be guessed
def vybrat_slovo(slovo):
    slovo = random.choice(open("slova_python.txt","r").read().split()) 
    return slovo

#the main function
def hrat_hru():
    global slovo
    global pokusy
    global spatne_pis
    global spravne_pis
    global konec_hry
    global zadane
    global spatne
    imgLabel.config(image=photos[spatne])
    pokusy = 0
    abeceda = 'abcdefghijklmnopqrstuvwxyz'
    slovo = str(vybrat_slovo(slovo))
    ukazani(spatne_pis, spravne_pis, slovo)
    uhadnuto = False
    max_pokusy = int(pokusy_hodnota(pokusy_combobox))
    pokusy = max_pokusy + 1
    while konec_hry==False and pokusy>0:
        pokusy_lb = Label(status_rm, text = int(pokusy), font=("Arial 22"))
        pokusy_lb.pack(side=RIGHT)
        global value
        #zadane = value.get()
        if pokusy == (max_pokusy + 1):
            zadane = ""
            pokusy = pokusy-1
        else:
            zadane = select(zadane)

#THIS IS THE LINE WHERE THE ERROR APPEARS

        if zadane in slovo:
            spravne_pis = spravne_pis + zadane
            uhadnuto = True
            for i in range(len(slovo)):
                if slovo[i] not in spravne_pis:
                    uhadnuto = False
                    hlaska = Label(dolni,text = ("Nice work! You guessed a letter. Keep up the good work!"), font = ("Arial 18")).grid(padx=90, pady=20)
                    break
                if uhadnuto:
                    konec_hry = True
                    hlaska = Label(dolni,text = ("Great job! You have succesfully guessed the word! The word was: ",slovo), font = ("Arial 18")).grid(padx=90, pady=20)
                    #hlaska.set("Great job! You have succesfully guessed the word! The word was: ",slovo)
        else:
            hlaska = Label(dolni,text = ("Sorry, the word does not contain this letter. Try guessing again"), font = ("Arial 18")).grid(padx=90, pady=20)
            spatne_pis = spatne_pis + zadane
            pokusy = pokusy - 1
            spatne =spatne + 1
            imgLabel.config(image=photos[spatne])
            if len(spatne_pis) == pokusy:
                konec_hry = True
                hlaska = Label(dolni,text = ("Sorry, you did not guess the word and were hanged as a result. Try playing again!",slovo), font = ("Arial 18")).grid(padx=90, pady=20)
        if konec_hry:
            messagebox.showinfo(text = "The game has ended.")
            break
main.mainloop()

Here is the full traceback:

Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/hanka/Škola/Hangman/polofungujici2.py", line 236, in <module>
    hrat_hru()
  File "/home/hanka/Škola/Hangman/polofungujici2.py", line 208, in hrat_hru
    if zadane in slovo:
TypeError: 'in <string>' requires string as left operand, not NoneType

I do not know how to fix this and will be very glad for any answers.

If you post the full traceback we can see which line it refers to.user56700
Please show a complete error message. It shows you all that other text for a reason. Copy and paste, starting from the line that says Traceback (most recent call last):, and format it like code. Also, try ericlippert.com/2014/03/05/how-to-debug-small-programs for some hints on tracking down errors, and minimal reproducible example for hints on isolating them.Karl Knechtel
@user56700 I added the whole traceback message.Hana Liškařová
Your select doesn't return anything, but you have zadane = select(zadane). What do you expect select(zadane) to be?John Coleman
@JohnColeman zadane is supposed to be a key that the user enters by pressing a minikeyboard i made using tkinter. It works only in the function select zadane but I am unable to use it anywhere else.Hana Liškařová