在python中编程时出现名称错误

2024-07-02 14:50:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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

我做错什么了?你知道吗


Tags: name程序名称checkfindelifgeorgekaterina
2条回答

你得到你的错误是因为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")

看看这本字典是如何让你的生活变得更轻松,代码更清晰的?阅读更多关于Python Data Structures。你知道吗

因此,有几件事你忽略了/没有按预期进行,第一件是how iterating over strings in python工作:

tilefwnikos_katalogos = "Christoforos 99111111: Eirini 99556677: Costas 99222222: George 99333333: Panayiotis 99444444: Katerina 96543217"
for check in tilefwnikos_katalogos:
    print(check)
    #print(repr(check)) #this shows it as you would write it in code ('HI' instead of just HI)

所以check永远不可能等于您正在检查它的任何东西,并且如果没有else语句,变量arxi永远不会被定义。我假设您打算使用来自用户输入的check,而不是循环中的check,但我不确定您是否需要循环:

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

尽管这会使Ct的输入给出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")

尽管如果您仍要解析字符串,并且可能会多次使用数据,但最好将数据打包到@BernardMeurer suggested这样的dict中:

data = {}
for entry in tilefwnikos_katalogos.split(":"):
    name, number = entry.strip().split(" ")
    data[name] = number #maybe use int(number)?

if check in data:
    apotelesma = data[check]
else:
    raise NotImplementedError("need a case where input is invalid")

相关问题 更多 >