かなり単純な質問だと思います...文字通り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")