写入文件直到我想停止

时间:2015-12-14 02:55:14

标签: python list file

我有3个输入:

 function moveNot() {
            $('#slider ul').animate({
                left: - slideWidth
            }, 20000, function () {
                $('#slider ul li:first-child').appendTo('#slider ul');
                $('#slider ul').css('left', '');
            });
        };



        $('#slider').mouseover(function () {

            moveNot();

        });

        $('#slider').mouseout(function () {

            setInterval(function () {
            moveRight();
        }, 1000);

        });

首先,我将这三个参数写入一个文件(空或填充了一些遗嘱)。程序问我之后"你想要添加更多吗?",如果我没有输入任何内容并按回车键,那么程序会将我的答案写入这种格式的文件中。

name = input(">> ")
price = input(">> ")
count = input(">> ")
print("\nDo you want to add more products?")
more = input(">> ")

如果我按任意键然后按回车键,程序将继续询问我3个输入。如果我在此之后停止,那么文件外观将如下所示:

name/price/count

我可以根据需要多次继续此序列。

到目前为止,这就是我所拥有的并且它正在发挥作用。

name/price/count/name/price/count

问题:如何按照描述使该程序正常工作?

1 个答案:

答案 0 :(得分:1)

看一下这个例子:

def readingWritting(filename, data):
    with open(filename, 'w') as f:
        f.write('/'.join(data))

dataOfInputs = []
more = 'yes'

while more != '':
    name = input(">> ")
    price = input(">> ")
    count = input(">> ")

    dataOfInputs.append(name)
    dataOfInputs.append(price)
    dataOfInputs.append(count)

    print("\nDo you want to add more products?")
    more = input(">> ")


readingWritting("someFile.txt", dataOfInputs)
print("\nSuccesifully!\n")

演示:

kevin@Arch ~> python test.py 
>> Kevin
>> 2
>> 23

Do you want to add more products?
>> yes
>> Bob
>> 33
>> 12

Do you want to add more products?
>> 

Succesifully!

kevin@Arch ~> cat someFile.txt 
Kevin/2/23/Bob/33/12
相关问题