所以我尝试将命令行待办事项列表作为Python的练习,从reddit获得这个想法。我的代码出了什么问题?我试图将'x'的值传递给函数'doAction',但即使x等于'1'或'2',它也会返回else语句。建议?谢谢。
答案:raw_input返回一个String对象,因此不等于我的switch语句
class To_Do(object):
def __init__(self):
self.itemList = []
def add_item(self, item):
self.itemList.append(item)
def view_list(self):
for number, thing in enumerate(self.itemList, start = 1):
print "{0}. {1}".format(number, thing)
todo1 = To_Do()
def getInput():
print "1.) Add Item\n2.)View List\n"
print "Enter choice:"
x = raw_input()
print x
return x
def doAction(x):
if(x == 1):
print "Enter input: "
todo1.add_item(raw_input())
print "Item added."
elif(x == 2):
todo1.view_list()
else:
print "Input invalid. Retry."
doAction(getInput())
doAction(getInput())