0

I am trying to make a stock list display and editor project. I am trying to let users input what product they have and quantity they have in stock but it isnt saving to the txt file.

Where am I going wrong I am not getting any errors.

def showStock(f):
    print(f.read())
    userDecide = input("""
To add items to your stock list please type 'ADD'
To remove items from your stock please type 'REMOVE'
To close application type 'CLOSE'
""")
    if userDecide.lower() == 'add':
        addStock(f)
    elif userDecide.lower() == 'remove':
        delStock()
    elif userDecide.lower() == 'close':
        print('Closing...')
        exit()
    else:
        print('Response does not match criteria.')

def addStock(f):
    item_add = input("""
Please enter the name of the item you wish to add.
: """)
    f.write(item_add+': ')
    quantityItem = input("""
Item added, please enter the current quantity for the item.
: 
""")
    f.write(quantityItem+'\n')
    userDecide = input("""
Quantity added. All information has been saved. If you need to;
If you need to add more items type 'ADD'
If you need to delete items type 'REMOVE'
If you would like to exit application type 'CLOSE'
""")
    if userDecide.lower() == 'add':
        addStock(f)
    elif userDecide.lower() == 'remove':
        delStock()
    elif userDecide.lower() == 'close':
        print('Closing...')
        exit()
    else:
        print('Response does not match criteria.')
    exit()

with open('CurrentStock.txt', 'w+') as f:
    print("""
    =====================================================
              Welcome to Max's stock project.
    =====================================================

    Current stock:
    """)

    showStock(f)
    addStock(f)
  • Doesn't 'Add Stock' just get into an infinite loop? – JeffUK Dec 12 '21 at 13:28
  • @JeffUK currently yes but that is because I haven't finished everything it won't be like that for much longer – SkiesLearns Dec 12 '21 at 13:30
  • I removed the addstock(f) on the last line it didn't need to be there. – SkiesLearns Dec 12 '21 at 13:34
  • 1
    I mean, if you add stock repeatedly you will potential crash with a max_recursion error. It seems the code does write to the file exactly as you would expect, the problem is that it doesn't read from the file. this is because you open it with w+ which resets the file! Try r+ – JeffUK Dec 12 '21 at 13:35
  • https://stackoverflow.com/questions/16208206/confused-by-python-file-mode-w – JeffUK Dec 12 '21 at 13:39
  • 1
    Changed to r+ and it works now!! Thank you I guess I misunderstood how w+ performs! – SkiesLearns Dec 12 '21 at 13:39

1 Answers1

0

Wrong mode changed from w+ to r+ and also removed addStock to prevent infinite loop.

def showStock(f):
    print(f.read())
    userDecide = input("""
To add items to your stock list please type 'ADD'
To remove items from your stock please type 'REMOVE'
To close application type 'CLOSE'
""")
    if userDecide.lower() == 'add':
        addStock(f)
    elif userDecide.lower() == 'remove':
        delStock()
    elif userDecide.lower() == 'close':
        print('Closing...')
        exit()
    else:
        print('Response does not match criteria.')

def addStock(f):
    item_add = input("""
Please enter the name of the item you wish to add.
: """)
    f.write(item_add+': ')
    quantityItem = input("""
Item added, please enter the current quantity for the item.
: 
""")
    f.write(quantityItem+'\n')
    userDecide = input("""
Quantity added. Info will be saved on close. If you need to;
If you need to add more items type 'ADD'
If you need to delete items type 'REMOVE'
If you would like to exit application type 'CLOSE'
""")
    if userDecide.lower() == 'add':
        addStock(f)
    elif userDecide.lower() == 'remove':
        delStock()
    elif userDecide.lower() == 'close':
        print('Closing...')
        exit()
    else:
        print('Response does not match criteria.')
    exit()

def delStock(f):
    #Working on

with open('CurrentStock.txt', 'r+') as f:
    print("""
    =====================================================
              Welcome to Max's stock project.
    =====================================================

    Current stock:
    """)

    showStock(f)

  • addstock will still call itself recursively. The fact you have copied the UI code in two different places is always a good sign you've done something wrong! Suggest you could have a 'While not Quit' loop, which 1. gets the users' request (or 'quit'), then 2. actions that request. – JeffUK Dec 12 '21 at 13:46
  • @JeffUK so when you say it will call itself reclusively how would that do so when from what I believe it is only called now if the users requests it to be done so. – SkiesLearns Dec 12 '21 at 14:36
  • The function will call itself if the user selects 'add' every time they select 'add' the function calls addstock again, the first call never completes until the next one does (that never complete until the next one etc. etc.) Using an increasing amount of memory etc. A default python installation will crash with a RecursionError before you can add about 1000 stocks – JeffUK Dec 12 '21 at 15:26