Search code examples
tkinterpython-3.6palindromenameerror

python 3 name 'user_input' is not defined


So i got this assignment where i'm supposed to create a palindrome detector and make a simple interface using tkinter. We also have to remove specifik characters and replace them with a blankspace. I also want to add that we are not allowed to use isalnum() Now the problem is the following:

File "Documents/palindrometkinter.py", line 71, in result
turn()
File "Documents/palindrometkinter.py", line 62, in turn
i = len(user_input)
NameError: name 'user_input' is not defined 

I'm really new when it comes to python so i'm not really good at it, but after hours of getting nowhere i'm asking for help. This is my code so far...

# -*- coding: UTF-8 -*-

import tkinter
import tkinter.messagebox


def main():
 settings()
 tkinter.mainloop()

def settings():
 main_window = tkinter.Tk()
 top_frame = tkinter.Frame()
 mid_frame = tkinter.Frame()
 bottom_frame = tkinter.Frame()


 main_window.title("Palindromdetektor")
 main_window.geometry("400x400")


 global label1
 label1 = tkinter.Label(top_frame, text = "Skriv in ett palindrom nedan 
                                       för att testa det!",
                           bg = "green", width = 60, height = 6)
 global button1
 button1 = tkinter.Button(mid_frame, text = "Testa palindrom", height = 
    3, width = 22, command = result)

 global button2
 button2 = tkinter.Button(bottom_frame, text ="Spara palindrom", height 
  = 3, width = 22, command = save)                                

 global button3
 button3 = tkinter.Button(bottom_frame, text ="Avsluta programmet", 
     height = 3, width = 22, command=main_window.destroy)

 global palindromentry
 palindromentry = tkinter.Entry(mid_frame, width = 67)
 palindromentry.pack()


 top_frame.pack()
 mid_frame.pack()
 bottom_frame.pack()

 label1.pack()
 button1.pack()
 button2.pack()
 button3.pack()

def entry():
 not_valid = " -,.!?:;'+()/" #The not accepted characters
 user_input = palindromentry.get()
 i=0
 while i < len(not_valid): 
    user_input = user_input.replace( not_valid[i], "")
    i = i + 1
 global backward_string
 backward_string = ""


def turn():
 i = len(user_input)
 while i > 0:
   backward_string += user_input[i-1] 
   i -=1
   if user_input == backward_string:
    equal = True     


def result():
 turn()
 entry()
 feedback_string = ""
 if (user_input == backward_string):
    label1.config(text="Ja, detta är ett palindrom!", bg="green") #when 
                                                      it's a palindrome

 elif (user_input != backward_string):
    label1.config(text="Det är inte en palindrom!", bg="red") #message 
                                          when it's not a palindrome

 else:
     label1.config(text="Hoppsan nu har något gått fel!")    


def save():

 if (user_input == backward_string):
    my_palindrome = open("palindrom.txt", "a") # if it's a 
                             palindrome you should be able to save it.
    my_palindrome.write(user_input + "\n")
    label1.config(text="Palindrom har sparats!")

 elif (user_input != backward_string):
    label1.config(text="Det gick inte att spara, måste vara en 
                         palindrom!")# you can't save it when it's not

 else:
    label1.config(text="Något gick fel!") # when something goes wrong





if __name__ == '__main__':
     main()

Solution

  • Your user_input variable is only defined int the entry function. You are trying to access a variable that doesn't exist in the current context (turn()).

    If your wish to be able to access a variable in every function, make it a global variable by defining it above everything else in the script.

    That or use your palindromeentry.get() everytime you wish to access it. Hope this helps you in getting to know python a little more.

    This might help: Using global variables