名称:name' trys1'未定义
在我的学校项目中,我正在比较搜索算法。对于36到65之间的搜索数字,我没有错误。在此数字范围以下/低于此错误发生的错误(nameRror:name'trys1'未定义)。我不知道为什么。我试图在定义后直接调用DEF,因为它已解决了类似的问题,但它不起作用。我应该怎么办?如果从DEF图中删除TRY1,则不再发生误差。
import math
from Suche.SucheZahlen import Zahlen
import matplotlib.pyplot as plt
index1 = []
index2 = []
index3 = []
data0 = Zahlen.data
length = len(data0) - 1
def suche(number, x, right, length, data0):
global index1
global trys1
runtime = int(math.log2(length))
for i in range(runtime + 1):
if data0[x] <= number <= data0[right]:
median_index = int((length + x) / 2 - 1)
index1.append(median_index)
if number == data0[median_index]:
print("ja")
trys1 = i + 1
break
if number - 1 > data0[median_index]:
x = median_index + 1
elif number - 1 < data0[median_index]:
length = median_index + 1
elif number - 1 == data0[median_index]:
print("ja")
break
else:
print("nein")
break
def intervallsuche(number, right, left, data0):
global trys2
for i in range(right):
if data0[left] <= number <= data0[right]:
x = left + (number - data0[left]) / (data0[right] - data0[left]) * (right - left)
round = x % 1
if round < 0.5:
x = int(x)
if round >= 0.5:
x = int(x) + 1
index2.append(x)
if data0[x] == number:
trys2 = i + 1
print("ja")
break
if data0[x] > number:
right = right - 1
if data0[x] < number:
left = left + 1
else:
print("nein")
break
def linearesuche(number, x, right, data0):
global trys3
if data0[x] <= number <= data0[right]:
for i in range(right):
if number == data0[x]:
trys3 = i
print("ja")
break
else:
x = x + 1
index3.append(x)
else:
print("nein")
def diagramm():
x1 = list(range(trys1))
x2 = list(range(trys2))
x3 = list(range(trys3))
x4 = [0, length]
y1 = index1
y2 = index2
y3 = index3
y4 = [number, number]
xmin = 0
xmax = trys3
ymin = 0
ymax = data0[100]
plt.axis([xmin, xmax, ymin, ymax])
plt.scatter(x1, y1)
plt.scatter(x2, y2)
plt.scatter(x3, y3)
plt.plot(x4, y4)
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
number = int(input("number:"))
linearesuche(number, 0, length, data0)
intervallsuche(number, length, 0, data0)
suche(number, 0, length, length, data0)
diagramm()
In my school project I am comparing search algorithms. I don't get an error for searched numbers between 36 and 65. Above/below this number range the error occurs (NameError: name 'trys1' is not defined). I don't know why. I tried to call the def directly after the definition as it was solved for a similar question but it doesn't work. What should I do? If try1 is removed from the def diagram, the error no longer occurs.
import math
from Suche.SucheZahlen import Zahlen
import matplotlib.pyplot as plt
index1 = []
index2 = []
index3 = []
data0 = Zahlen.data
length = len(data0) - 1
def suche(number, x, right, length, data0):
global index1
global trys1
runtime = int(math.log2(length))
for i in range(runtime + 1):
if data0[x] <= number <= data0[right]:
median_index = int((length + x) / 2 - 1)
index1.append(median_index)
if number == data0[median_index]:
print("ja")
trys1 = i + 1
break
if number - 1 > data0[median_index]:
x = median_index + 1
elif number - 1 < data0[median_index]:
length = median_index + 1
elif number - 1 == data0[median_index]:
print("ja")
break
else:
print("nein")
break
def intervallsuche(number, right, left, data0):
global trys2
for i in range(right):
if data0[left] <= number <= data0[right]:
x = left + (number - data0[left]) / (data0[right] - data0[left]) * (right - left)
round = x % 1
if round < 0.5:
x = int(x)
if round >= 0.5:
x = int(x) + 1
index2.append(x)
if data0[x] == number:
trys2 = i + 1
print("ja")
break
if data0[x] > number:
right = right - 1
if data0[x] < number:
left = left + 1
else:
print("nein")
break
def linearesuche(number, x, right, data0):
global trys3
if data0[x] <= number <= data0[right]:
for i in range(right):
if number == data0[x]:
trys3 = i
print("ja")
break
else:
x = x + 1
index3.append(x)
else:
print("nein")
def diagramm():
x1 = list(range(trys1))
x2 = list(range(trys2))
x3 = list(range(trys3))
x4 = [0, length]
y1 = index1
y2 = index2
y3 = index3
y4 = [number, number]
xmin = 0
xmax = trys3
ymin = 0
ymax = data0[100]
plt.axis([xmin, xmax, ymin, ymax])
plt.scatter(x1, y1)
plt.scatter(x2, y2)
plt.scatter(x3, y3)
plt.plot(x4, y4)
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
number = int(input("number:"))
linearesuche(number, 0, length, data0)
intervallsuche(number, length, 0, data0)
suche(number, 0, length, length, data0)
diagramm()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,一些代码反馈:您几乎绝不应该将全局变量用于任何事物。实际上,您实际上需要它们非常罕见,并且如果您不小心,它们会导致各种名称空间问题 - 这就是一个例子。
实际问题:您实际上并未在
suche
函数之外创建一个名为trys1
的全局变量。全局变量需要在全局名称空间上定义,然后在函数中调用它们,只是告诉Python您要修改该全局变量,而不是新的本地变量。如果这令人困惑,希望下面的示例能澄清。以下代码将返回2,因为您告诉
foo
函数,您正在尝试修改上面定义的c
,而不是本地c
c 代码>您在功能的范围内创建的代码>。这次,以下代码应给出您收到的相同
nameError
(尽管我尚未测试),因为您正在尝试说出foo
函数以修改全局变量c
,但没有全局变量c
。现在,以下代码将简单地修改函数内部的
c
变量。First of all, some code feedback: You should almost never use global variables for anything. It is extremely rare that you'll actually need them, and they cause all sorts of namespace problems if you aren't careful - this being an example.
The actual issue: You haven't actually created a global variable called
trys1
outside of thesuche
function. Global variables need to be defined on the global namespace, and then calling them inside the function simply tells Python that you're trying to modify that global variable, rather than a new local one.If that's confusing, hopefully the example below will clarify. The below code will return 2, because you told the
foo
function that you're trying to modify thec
that you defined above, not a localc
that you created within the confines of the function.This time, the below code should give the same
NameError
that you received (although I haven't tested), because you're trying to tell thefoo
function to modify the global variablec
, but there is no global variablec
.Now, the below code will simply modify the
c
variable inside the function.