写入文本文件不会保存用户输入 PYTHON
Writing in a text file wont save users input PYTHON
我正在尝试制作股票列表显示和编辑器项目。我试图让用户输入他们有什么产品和他们有库存的数量,但它没有保存到 txt 文件。
我哪里出错了我没有收到任何错误。
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)
错误模式从 w+ 更改为 r+ 并且还删除了 addStock 以防止无限循环。
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)
我正在尝试制作股票列表显示和编辑器项目。我试图让用户输入他们有什么产品和他们有库存的数量,但它没有保存到 txt 文件。
我哪里出错了我没有收到任何错误。
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)
错误模式从 w+ 更改为 r+ 并且还删除了 addStock 以防止无限循环。
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)