使用exec定义并从变量获取值会引发NameError

使用exec定义并从变量获取值会引发NameError

本文介绍了使用exec定义并从变量获取值会引发NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在一个项目中,该项目将询问跑步者姓名(分别为1英里,2英里和5英里),并以此为基础生成数据.要求是要有足够的条目供7个跑步者使用,但我想我可以通过使用 exec()添加添加任意数量的跑步者的功能.但是,当尝试从其中定义的变量读取数据时,我得到一个 NameError:未定义名称'entry2',但没有为 entry0 entry1 ,所以我想知道是否有人可以帮助我解决这些问题.如果有帮助,我不一定需要使用 exec()来创建它们,但这是我所知道的可以定义变量并随后从中读取的内容.

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.

所有代码:

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

这是完整的错误日志:

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

推荐答案

万一有人在意,那是因为我使用的是旧变量 i ,并且没有将其设置为 0或使用其他变量.

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.

这篇关于使用exec定义并从变量获取值会引发NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:59