python 3名称'user_input'未定义

时间:2018-01-14 22:42:56

标签: tkinter python-3.6 palindrome nameerror

所以我得到了这个任务,我应该创建一个回文检测器并使用tkinter创建一个简单的接口。我们还必须删除特定字符并用空格替换它们。我还想补充一点,我们不允许使用isalnum() 现在问题如下:

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 

对于python来说,我真的很新,所以我并不擅长它,但经过几个小时无处可寻,我正在寻求帮助。 到目前为止,这是我的代码......

# -*- 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()

1 个答案:

答案 0 :(得分:1)

您的 user_input 变量仅在entry函数中定义。 您正在尝试访问当前上下文中不存在的变量(turn())。

如果您希望能够访问每个函数中的变量,请将其定义为脚本中其他所有内容的全局变量。

每次您希望访问它时使用palindromeentry.get()。 希望这有助于您更多地了解python。

这可能会有所帮助:Using global variables