0

I have 3 inputs:

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

First of all, I write these three parameters into a file (empty or filled with something nevermind). After the program asks me "Do you want to add more?", if I do not type anything and press enter, then the program will write my answers into a file with this format.

name/price/count

If I press any key and hit enter then the program will continue asking me for 3 more inputs. If I stop after this then the file look will look like this:

name/price/count/name/price/count

And I can continue this sequence as many times as needed.

So far, this is what I have and it it working.

readingWriting("someFile.txt", dataOfInputs)
print("\nSuccessfully!\n")

Question: How can I make this program work as described?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

1

Take a look at this example:

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

Demo:

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
Remi Guan
  • 21,506
  • 17
  • 64
  • 87