Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 制作列表并检索它_Python_List_Python 2.7 - Fatal编程技术网

Python 制作列表并检索它

Python 制作列表并检索它,python,list,python-2.7,Python,List,Python 2.7,下面是创建带有姓名和金额的费用列表的一些代码: def make_list(): expense_list = [] for count in range(1, 5): print "expense number" + str(count) + ":" name = raw_input(' enter expense name: ') amoun

下面是创建带有姓名和金额的费用列表的一些代码:

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()

我做错了什么?即使在交互模式下,我似乎也不知道如何获取我的项目。

您的缩进是错误的,您实际上从未向列表中添加任何内容,也没有将返回的列表分配给任何内容。此外,“幻数”(
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

我还想说,“制造清单”不是一个很有描述性的名字;相反,AXEU5应该考虑一些“QueQueCurror”,谢谢JunrSape——你能详细解释一下吗?我对Python比较新。P@axeu5究竟什么是不清楚的?是否有特定的行或概念您不理解?
范围(1,n+1)循环应该何时结束?
您测试过吗?添加一个
print count
,看看会发生什么。但是,即使我正在使用您建议我做的事情,让我们说:
my_list=make_list()print my_list
输出是:“[]”