【发布时间】:2020-09-22 03:31:14
【问题描述】:
我现在遇到了一个错误(首先我已经知道使用exec 不是最好的选择,但我现在不想这样做)关于在引用它时未定义的变量。在我添加使用标准时间格式 (1:36) 的功能之前,考虑到当我使用 exec(f'oneMileMark.append(entry{i}.get())')(此时它已经是一个浮点数)时它工作得非常好,我觉得这很奇怪。
for i in range(numOfRunners):
if i%4 == 0: # used to distinguish between a runner's name and different times
exec(f'time = entry{i}.get()') # gets the value of entry{i} and saves it as time
minutes,seconds=time.split(':') # splits time into mins and secs
newTime=float(minutes) + float(seconds)/60 # combines the minutes and hours into one bariable
oneMileMark.append(newTime) # adds newTime to a list
这给出了错误:
Traceback (most recent call last):
File "/Users/Me/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 91, in <module>
app = Application(root)
File "/Users/Me/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 12, in __init__
self.get_runner_data()
File "/Users/Me/Desktop/Computer Programming/Python 3.x/Assignments/Programming 2/8/9_15 Computing 5k Mile Splits/main.py", line 53, in get_runner_data
hours,minutes=time.split(':')
NameError: name 'time' is not defined
【问题讨论】: