[英]How to understand an undefined error in a function I'm trying to write
I'm a new engineer and I'm writing a simple phone book app in Python. It's pretty self explanatory, as it's a beginner project.我是一名新工程师,我正在 Python 中编写一个简单的电话簿应用程序。这是一个非常不言自明的项目,因为它是一个初学者项目。 When I was writing the function I forgot to tell it what to do if a entry that was being search wasn't found.
当我写 function 时,我忘记告诉它如果没有找到正在搜索的条目该怎么办。
I have looked at several examples, and to the best of my growing knowledge base, coded what I thought was correct.我看过几个例子,并根据我不断增长的知识库,对我认为正确的代码进行了编码。 I am getting an error and would like to understand it.
我收到一个错误,想了解它。
Also, optimization is key, so one of my objectives is to learn to code for optimization the first time.此外,优化是关键,所以我的目标之一是第一次学习编写优化代码。
Traceback (most recent call last):
File "/Users/corcoding/Desktop/projects/phonebook-project/phonebook.py", line 20, in <module>
print("phone number of" ,name1, "is", d1==[name])
NameError: name 'name' is not defined
Code:代码:
def menu():
print("-------Lil Black Book--------")
print("[1] Look up an entry")
print("[2] Set an entry")
print("[3] delete an entry")
print("[4] List all entries")
print("[5] Quit")
print("what would you like to do (1-5)?")
menu()
d1 = {}
while True:
n=int(input("enter number [1-5]:-"))
if n ==2:
name=input("enter name:-")
phono=(input("enter phone number:-"))
d1[name]=phono
elif n==1:
name1=input("enter name to SEARCH for phone number in the phone book")
print("phone number of" ,name1, "is", d1[name])
if name1 != d1[name]:
print("entry not found")
if n== 3:
name1=input("enter name to delete:-")
d1.pop(name)
elif n==5:
break
I believe this code may accomplish what you are looking for:我相信这段代码可以完成你正在寻找的东西:
d1 = {}
while True:
n = int(input("enter number [1-5]:-"))
if n == 1:
name1 = input("enter name to SEARCH for phone number in the phone book")
if not(name1 in d1.keys()):
name1 = input("please enter someone who is already in your book")
else:
print("phone number of" ,name1, "is", d1[name1])
elif n == 2:
name = input("enter name:-")
phono = (input("enter phone number:-"))
d1[name]=phono
elif n == 3:
name1 = input("enter name to delete:-")
d1.pop(name)
elif n == 4:
print(d1)
elif n == 5:
break
First off, I ordered the if
statements by n
so it is easier to read.首先,我将
if
语句按n
排序,这样更容易阅读。
Note that indenting does matter in Python!请注意,缩进在 Python 中很重要! Whatever you want to run inside of an
if
statement must be indented inside of it.无论您想在
if
语句中运行什么,都必须在其中缩进。
The main issue is when n = 1
.主要问题是什么时候
n = 1
。 This is because we do not know if name1
exists inside of d1
.这是因为我们不知道
name1
是否存在于d1
中。 Therefore, we must first check if name1
exists inside of d1
before calling d1[name1]
.因此,在调用
d1[name1]
之前,我们必须首先检查d1
中是否存在name1
。
Moved up the validation and changed the code.向上移动验证并更改代码。
def menu():
print("-------Lil Black Book--------")
print("[1] Look up an entry")
print("[2] Set an entry")
print("[3] delete an entry")
print("[4] List all entries")
print("[5] Quit")
print("what would you like to do (1-5)?")
menu()
d1 = {}
while True:
n = int(input("enter number [1-5]:-"))
if n == 2:
name = input("enter name:-")
phono = (input("enter phone number:-"))
d1[name] = phono
elif n == 1:
name1 = input(
"enter name to SEARCH for phone number in the phone book")
if name1 != d1.get(name1,None): # Validate name exist
print("entry not found")
else:
print("phone number of", name1, "is", d1[name])
if n == 3:
name1 = input("enter name to delete:-")
d1.pop(name)
elif n == 5:
break
Test run.测试运行。
[1] Look up an entry
[2] Set an entry
[3] delete an entry
[4] List all entries
[5] Quit
what would you like to do (1-5)?
enter number [1-5]:-1
enter name to SEARCH for phone number in the phone bookDoe
entry not found
enter number [1-5]:-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.