Using exec to define and get the value from variables raises NameError

自闭症网瘾萝莉.ら 提交于 2021-01-29 14:53:02

问题


Right now I'm working on a project that will ask for a runner's name, 1 mile, 2 mile, and 5 miles times and generate data based off that. The requirement is to have enough entries for 7 runners but I thought I'd add the ability to add any number of runners by using exec(). However, when trying to read data from the variables defined within them, I get a NameError: name 'entry2' is not defined but not for entry0 and entry1, so I was wondering if anyone could help me out on these. In case it helps, I don't necessarily need to use exec() to create them but it was what I knew could define variables and later read from them.

All code:

from UsefulFunctions import setup_screen, clear_window # just some of my functions, pretty obvious what they do
from tkinter import *

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()
        
    def create_widgets(self):
        Label(self,text='Enter how many runners you\' entering times for:').grid()
        numOfRunners=Entry(self);numOfRunners.grid()
        var = IntVar()
        test = Button(self, text = "Submit", command = lambda: var.set(1)); test.grid()
        while True:
            try:
                test.wait_variable(var)
                numOfRunners=int(numOfRunners.get())
                break
            except ValueError:
                Label(self,text='Make sure that you use a number like \'4\', not \'four\'!').grid(row=3)
        clear_window(self)
        
        Label(self,text='Input').grid(row=0,column=0,sticky=W)
        for i in range(numOfRunners):
            columnNum=0
            Label(self,text='Runner Name').grid(row=(i*2)+1,column=0,sticky=W)
            Label(self,text='1 Mile Mark Time').grid(row=(i*2)+1,column=1,sticky=W)
            Label(self,text='2 Mile Mark Time').grid(row=(i*2)+1,column=2,sticky=W)
            Label(self,text='5k Time').grid(row=(i*2)+1,column=3,sticky=W)
            for col in range(4):
                exec('entry%s=Entry(self);entry%s.grid(row=%s,column=%s)' % (str(i),str(i),str((i*2)+2),str(col)))
        runnerNames=[]
        oneMile=[]
        twoMile=[]
        fiveK=[]
        var = IntVar()
        submitButton = Button(self, text = "Submit", command = lambda: var.set(1)); submitButton.grid(sticky=W)
        submitButton.wait_variable(var)
        
        for i in range(numOfRunners*4):
            exec("print('entry%s.get() = ' + entry%s.get())" % (i,i))
        
        self.calculate(runnerNames,oneMile,twoMile,fiveK)
        
    def calculate(self,runnerNames,oneMile,twoMile,fiveK):
        print(runnerNames)
        print(oneMile)
        print(twoMile)
        print(fiveK)
        
root =Tk()
root.title("AT Pace Check Times")
setup_screen(root, 800, 400)
app = Application(root)
root.mainloop()

Here's the full error log:

Traceback (most recent call last):
  File "/Users/122224/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 70, in <module>
    app = Application(root)
  File "/Users/122224/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 12, in __init__
    self.create_widgets()
  File "/Users/122224/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 48, in create_widgets
    exec("print('entry%s.get() = ' + entry%s.get())" % (i,i))
  File "/Users/122224/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 1, in <module>
    try:
NameError: name 'entry2' is not defined

回答1:


In case anyone cares it was because I was using an older variable i and didn't either set it to 0 or use another variable.



来源:https://stackoverflow.com/questions/63940984/using-exec-to-define-and-get-the-value-from-variables-raises-nameerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!