在python中编程时发生NameError

在python中编程时发生NameError,python,Python,我用python编写了一个程序,应该接受一个名称作为用户输入。然后,它将检查给定的名称是否包含在已给定的字符串中,如果是,则程序将打印出该名称旁边的电话。我的代码如下: tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217" check=str(input("Give a na

我用python编写了一个程序,应该接受一个名称作为用户输入。然后,它将检查给定的名称是否包含在已给定的字符串中,如果是,则程序将打印出该名称旁边的电话。我的代码如下:

tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
check=str(input("Give a name: "))
for check in tilefwnikos_katalogos:
  if check=="Christoforos":
    arxi=check.find("Christoforos")
  elif check=="Eirini":
    arxi=check.find("Eirini")
  elif check=="Costas":
    arxi=check.find("Costas")
  elif check=="George":
    arxi=check.find("George")
  elif check=="Panayiotis":
    arxi=check.find("Panayiotis")
  elif check=="Katerina":
    arxi=check.find("Katerina")
  s=check.find(" ",arxi)
  arxi=s
  y=check.find(":",arxi)
  telos=y
apotelesma=tilefwnikos_katalogos[arxi+1:telos]
print(apotelesma)
但当我尝试运行它时,我输入了名称,然后弹出以下消息:

Traceback (most recent call last):

File "C:\Users\Sotiris\Desktop\test.py", line 16, in <module> s=check.find(" ",arxi)

NameError: name 'arxi' is not defined
回溯(最近一次呼叫最后一次):
文件“C:\Users\Sotiris\Desktop\test.py”,第16行,在s=check.find(“,arxi)中
NameError:未定义名称“arxi”

我做错了什么?

你得到了错误,因为当用户给出的名字不在你的列表中时,
arxi
首先没有得到定义。你可以通过简单地将无条件的
else
案例添加到你的
if
/
else if
捆绑包中,如注释中所述,来解决这个问题。但是您解决此问题的方法本身是错误的,将这样的数据存储在字符串中是一个不好的想法,您想使用字典:

phone_catalog = {'Christoforos': 99111111, 'Eirini': 99556677, 'Costas': 99222222, 'George':99333333, 'Panayiotis':99444444, 'Katerina': 96543217}
另外,
check
不是一个非常清晰的变量名,也许您应该尝试使用更好的名称,如:

user_name = str(input("Give a name: "))
现在,您可以执行
if
/
elif
条件,但替换它是为了使用字典逻辑并确保您有最终的else,如:

if user_name in phone_catalog:
    print(phone_catalog[user_name])
else:
    print("Unknown user")

看看这本字典是如何让你的生活变得更轻松,代码也更简洁的吧?阅读更多信息。

您之所以会收到错误,是因为当用户提供的名称不在您的列表中时,
arxi
首先没有得到定义。您可以通过向注释中指出的
if
/
else if
捆绑包添加无条件的
else
案例来修复此问题。但是您解决此问题的方法本身是错误的,将这样的数据存储在字符串中是一个不好的想法,您想使用字典:

phone_catalog = {'Christoforos': 99111111, 'Eirini': 99556677, 'Costas': 99222222, 'George':99333333, 'Panayiotis':99444444, 'Katerina': 96543217}
另外,
check
不是一个非常清晰的变量名,也许您应该尝试使用更好的名称,如:

user_name = str(input("Give a name: "))
现在,您可以执行
if
/
elif
条件,但替换它是为了使用字典逻辑并确保您有最终的else,如:

if user_name in phone_catalog:
    print(phone_catalog[user_name])
else:
    print("Unknown user")

看看这本字典是如何让你的生活变得更轻松,代码也更简洁的吧?阅读更多信息。

因此,有几件事你忽略了/没有按预期进行,其中第一件是有效的:

因此,
check
永远不能等于您要检查的任何内容,如果没有
else
语句,变量
arxi
永远不会定义。我假设您打算使用来自用户输入的
检查
,而不是循环中的检查,但我不确定您是否需要循环:

tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
check=str(input("Give a name: ")) #the str() isn't really necessary, it is already a str.

if check=="Christoforos":
    arxi=check.find("Christoforos")
elif check=="Eirini":
    arxi=check.find("Eirini")
elif check=="Costas":
    arxi=check.find("Costas")
elif check=="George":
    arxi=check.find("George")
elif check=="Panayiotis":
    arxi=check.find("Panayiotis")
elif check=="Katerina":
    arxi=check.find("Katerina")
else: raise NotImplementedError("need a case where input is invalid")
s=check.find(" ",arxi)
arxi=s
y=check.find(":",arxi)
telos=y
apotelesma=tilefwnikos_katalogos[arxi+1:telos]
print(apotelesma)
但您也可以查看
check
是否是
tilefwnikos_katalogos
的子字符串,并处理其他条件:

if check.isalpha() and check in tilefwnikos_katalogos:
    #    ^                  ^ see if check is within the string
    #    ^ make sure the input is all letters, don't want to accept number as input
    arxi=check.find(check)
else:
    raise NotImplementedError("need a case where input is invalid")
虽然这会输入
C
t
Cristoforos
”数字,因为它会检索第一次出现的字母。包含循环(但不调用变量
check
!)的另一种方法是将字符串拆分为一个列表:

tilefwnikos_katalogos = "..."
check = input(...)
for entry in tilefwnikos_katalogos.split(":"):
    name, number = entry.strip().split(" ")
    if check == name:
        apotelesma=number
        break
else:
    raise NotImplementedError("need a case where input is invalid")
尽管如果您仍要解析字符串,并且可以多次使用数据,但最好将数据打包到dict中,如:


因此,有几件事你忽略了/没有按预期进行,其中第一件是有效的:

因此,
check
永远不能等于您要检查的任何内容,如果没有
else
语句,变量
arxi
永远不会定义。我假设您打算使用来自用户输入的
检查
,而不是循环中的检查,但我不确定您是否需要循环:

tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
check=str(input("Give a name: ")) #the str() isn't really necessary, it is already a str.

if check=="Christoforos":
    arxi=check.find("Christoforos")
elif check=="Eirini":
    arxi=check.find("Eirini")
elif check=="Costas":
    arxi=check.find("Costas")
elif check=="George":
    arxi=check.find("George")
elif check=="Panayiotis":
    arxi=check.find("Panayiotis")
elif check=="Katerina":
    arxi=check.find("Katerina")
else: raise NotImplementedError("need a case where input is invalid")
s=check.find(" ",arxi)
arxi=s
y=check.find(":",arxi)
telos=y
apotelesma=tilefwnikos_katalogos[arxi+1:telos]
print(apotelesma)
但您也可以查看
check
是否是
tilefwnikos_katalogos
的子字符串,并处理其他条件:

if check.isalpha() and check in tilefwnikos_katalogos:
    #    ^                  ^ see if check is within the string
    #    ^ make sure the input is all letters, don't want to accept number as input
    arxi=check.find(check)
else:
    raise NotImplementedError("need a case where input is invalid")
虽然这会输入
C
t
Cristoforos
”数字,因为它会检索第一次出现的字母。包含循环(但不调用变量
check
!)的另一种方法是将字符串拆分为一个列表:

tilefwnikos_katalogos = "..."
check = input(...)
for entry in tilefwnikos_katalogos.split(":"):
    name, number = entry.strip().split(" ")
    if check == name:
        apotelesma=number
        break
else:
    raise NotImplementedError("need a case where input is invalid")
尽管如果您仍要解析字符串,并且可以多次使用数据,但最好将数据打包到dict中,如:


如果
check==“foo”
…?
arxi
没有设置,您需要在最后一次
elif
之后使用最后一次
else
语句。否则可能无法定义
arxi
。如果
check
不是这些名称中的任何一个,会发生什么情况<代码>arxi将不被定义。你需要添加一个
else:
来给
arxi
一个默认值。如果
check=='foo'
怎么办?
arxi
没有得到设置你需要在你最后一次
elif
之后有一个最终的
else
语句。否则可能无法定义
arxi
。如果
check
不是这些名称中的任何一个,会发生什么情况<代码>arxi将不被定义。您需要添加一个
else:
来为
arxi
提供一个默认值。我可能会添加一个注释,说明他们为什么会首先得到错误,只是为了至少直接解决OP最初的问题。最终,这并没有解决OP所面临的实际问题。@Noelkd No,这并不意味着代码改进无法指向。OP在这里使用的是一个完全kaput的数据结构,教他有关字典没有坏处,我只是不明白为什么你会忽略实际问题,而不是先处理它,然后提供improvements@Noelkd我看到了这个错误,编辑了这个问题,也就是说,他的问题已经在评论中指出了。我可能会补充一点,说明他们为什么会在一开始就犯错误,至少是为了直接解决问题