我想知道如何在没有全局变量的情况下创建此代码。
我已经尝试了自己,但似乎涉及返回,但之后它不会回到“菜单”(main_list)。此代码的要点是始终返回菜单,除非按“3”(退出程序)。
对于大(和坏)代码感到抱歉,我感谢所有帮助。
import sys
word = []
desc = []
def main_list():
print "\nMenu for list \n"
print "1: Insert"
print "2: Lookup"
print "3: Exit program"
choice = raw_input()
print "Choose alternative: ", choice
if choice.isdigit():
choice = int(choice)
if choice == 1:
insert()
elif choice == 2:
look()
elif choice == 3:
sys.exit()
else:
print "Error: Not a valid choice \n", main_list()
else:
print "Error: Not a valid choice \n", main_list()
def insert():
ins = raw_input("Word to insert: ")
if ins not in word:
word.append (ins)
else:
print "Error: Word already exist \n", main_list()
desc.append(raw_input ("Description of word: "))
main_list()
def look():
up = raw_input("Word to lookup: ")
if up not in word:
print "Error: Word not found \n", main_list()
i = 0
while up != word[i]:
i += 1
if up == word[i]:
print "Description of word: ", desc[i]
main_list()
答案 0 :(得分:1)
看起来你应该在main函数中使用while循环,这样它只会在你想要的时候退出:
这样的事情:
event
编辑:正如下面的Prune所说的那样,我没有给出任何理由来答案,所以这里有:
您的代码没有返回到您想要的循环的原因是您使用if语句来运行循环。 while循环将允许您重复所需的过程,直到您需要中断。如果您想要一个不使用从其他函数调用的main_list()函数的理由,请查看Hosch250的答案
答案 1 :(得分:1)
正如Xeno所说,你需要一个while
循环来连续循环输入。对于您的情况,我建议使用do-while
循环,但Python没有内置do-while
,因此您需要模拟一个,可能是这样的:
while True:
# do stuff
if condition:
break
要摆脱全局变量,您需要将变量传递给您的方法并返回它们。
def insert(word, desc):
# do stuff
现在,我注意到您在main_list()
和insert()
结束时致电look()
。不要这样做。每次都不需要新实例,需要返回当前实例。所以,设置这样的东西:
def main_list():
# do stuff
while True:
# do more stuff
if condition:
break
# do more stuff
def insert():
# do stuff - return any new value; otherwise, just let it auto-return
def look():
# do stuff - return any new value; otherwise, just let it auto-return
答案 2 :(得分:0)
首先,按照之前的“回答”建议清理主循环:删除exit子句,并在完成时保留while循环。
其次,在参数列表中传递word和desc。将它们添加到函数中的“def”行。
第三,从print语句中删除对main_list的调用;当你跑掉函数的底部时,你将返回主程序。
这会让你感动吗?
word = []
desc = []
menu = \
"\nMenu for list \n" \
"1: Insert\n" \
"2: Lookup\n" \
"3: Exit program"
choice = raw_input(menu)
while choice != 3:
if choice.isdigit():
choice = int(choice)
if choice == 1:
insert(word, desc)
elif choice == 2:
look(word, desc)
else:
print "Error: Not a valid choice \n", main_list()
else:
print "Error: Not a valid choice \n", main_list()
答案 3 :(得分:0)
对现有代码来说,最简单的事情可能就是重构它,这会使main_list()
通过向其添加while
循环来驱动整个过程,并让它通过将每个其他函数的共享变量作为参数。
def main_list():
word = []
desc = []
print "\nMenu for list"
print " 1: Insert"
print " 2: Lookup"
print " 3: Exit program"
while True:
choice = raw_input()
print "Alternative chosen: ", choice
if choice.isdigit():
choice = int(choice)
if choice == 1:
insert(word, desc)
elif choice == 2:
look(word, desc)
elif choice == 3:
break
else:
print "Error: Not a valid choice"
else:
print "Error: Not a valid choice"
def insert(word, desc):
ins = raw_input("Word to insert: ")
if ins not in word:
word.append(ins)
else:
print "Error: Word already exist"
desc.append(raw_input("Description of word: "))
def look(word, desc):
up = raw_input("Word to lookup: ")
if up not in word:
print "Error: Word not found"
i = 0
while up != word[i]:
i += 1
if up == word[i]:
print "Description of word: ", desc[i]
main_list()
答案 4 :(得分:-1)
将其封装在class
中。这样,单词列表可以保存在类实例中。它不是全球性的,你不需要传递它。
class main_list(object):
def __init__(self):
self.words = {}
def run(self):
while(True):
print "\nMenu for list \n"
print "1: Insert"
print "2: Lookup"
print "3: Exit program"
choice = raw_input()
print "Chose alternative: ", choice
if choice.isdigit():
choice = int(choice)
if choice == 1:
self.insert()
elif choice == 2:
self.look()
elif choice == 3:
break
else:
print "Error: Not a valid choice"
else:
print "Error: Not a valid choice"
def insert(self):
ins = raw_input("Word to insert: ").lower()
if ins not in self.words:
desc = raw_input("Enter description of word: ")
self.words[ins] = desc
else:
print "Error: Word already exist"
def look(self):
up = raw_input("Word to lookup: ").lower()
if up in self.words:
print "description of `%s` is `%s`" % (up, self.words[up])
else:
print "Error: Word %s not found" % up
ml = main_list()
ml.run()
请注意,我更改为使用字典的代码。这样可以避免需要两个单独的列表来保留word
和description
并提供更快的查找。