以下是一些代码,用于创建名称和金额的费用清单:
def make_list():
expense_list = []
for count in range(1, 5):
print "expense number" + str(count) + ":"
name = raw_input(' enter expense name: ')
amount = raw_input(' enter expense amount: ')
return expense_list
make_list()
我做错了什么?即使在交互模式下,我似乎无法弄清楚如何获取我的物品。
答案 0 :(得分:1)
您的缩进是错误的,您实际上从未在列表中添加任何内容,也不会将返回的列表分配给任何内容。此外,"幻数" (5
)并不理想。尝试:
def make_list(n):
expense_list = []
for count in range(1, n+1):
print "expense number {0}:".format(count)
name = raw_input(' enter expense name: ')
amount = raw_input(' enter expense amount: ')
expense_list.append((name, amount))
return expense_list
l = make_list(4)
print l