相当简单的问题,我想......我刚刚安装了 Python 并且正在测试一些初学者教程。
我想创建一个菜单,允许您将项目添加到列表中,然后检查它们是否已添加:测试功能和过程中的列表。
#create empty list and define variables
firstlist = {'joe'}
additem = "test"
printthis = "test"
#create menu, add or check name
def menu():
#print what options you have
print "Add to list: type '1'"
print "Check for name: type '2'"
print "To exit program: type '3'"
return input ("Choose your option: ")
def addmenu():
additem = input("Name of list item: ")
firstlist.append(additem)
print additem, "has been appended"
def checkmenu():
printthis = input ("What are you looking for?: ")
if firstlist.has_key(printthis):
print "is in the list"
else:
print "is not in the list"
# Perform action
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
addmenu()
elif choice == 2:
checkmenu()
elif choice == 3:
loop = 0
elif choice > 3:
print "You made an incorrect selection"
这是我的错误:
Traceback (most recent call last):
File "C:\Python27\testing python\tutorials\dictionaryselection", line 32, in <module>
addmenu()
File "C:\Python27\testing python\tutorials\dictionaryselection", line 15, in addmenu
additem = input("Name of list item: ")
File "<string>", line 1, in <module>
NameError: name 'TESTING' is not defined
不知道发生了什么...任何帮助将不胜感激。
下面的工作代码:转换为 python 3.x
#create empty list and define variables
firstlist = ['Joe']
additem = "test"
printthis = "test"
#create menu, add or check name
def menu():
#print what options you have
print ("")
print ("Add to list: type '1'")
print ("Check for name: type '2'")
print ("To list the whole list '3'")
print ("To exit program: type '4'")
print ("-------------------------")
return input ("Choose your option: ")
def addmenu():
additem = input("Name of list item: ")
firstlist.append(additem)
print (additem, "has been appended")
def checkmenu():
printthis = input("What are you looking for?: ")
if printthis in firstlist:
print ("is in the list")
else:
print ("is not in the list")
def listlist():
print (firstlist[1])
# Perform action
loop = 1
choice = 0
while loop == 1:
choice = int(menu())
if choice == 1:
addmenu()
elif choice == 2:
checkmenu()
elif choice == 3:
listlist()
elif choice == 4:
loop = 0
elif (choice > 4):
print ("You made an incoorect selection")