【问题标题】:Writing in a text file wont save users input PYTHON [duplicate]写入文本文件不会保存用户输入 PYTHON [重复]
【发布时间】:2021-12-12 13:22:39
【问题描述】:

我正在尝试制作股票列表显示和编辑器项目。我试图让用户输入他们拥有的产品和库存数量,但它没有保存到 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)

【问题讨论】:

  • “添加库存”不是陷入无限循环吗?
  • @JeffUK 目前是的,但那是因为我还没有完成所有事情,不会再这样了
  • 我删除了最后一行不需要的 addstock(f)。
  • 我的意思是,如果您反复添加库存,您可能会因 max_recursion 错误而崩溃。似乎代码确实完全按照您的预期写入文件,问题是它没有从文件中读取。这是因为您使用 w+ 打开它会重置文件!试试 r+
  • 已更改为 r+,现在可以使用了!!谢谢,我想我误解了 w+ 的表现!

标签: python file


【解决方案1】:

错误模式从 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)

【讨论】:

  • addstock 仍会递归调用自身。您在两个不同的地方复制了 UI 代码这一事实始终是您做错了什么的好兆头!建议您可以有一个“While not Quit”循环,其中 1. 获取用户的请求(或“退出”),然后 2. 请求的操作。
  • @JeffUK 所以当你说它会隐秘地调用自己时,我认为只有在用户要求这样做时才会调用它。
  • 如果用户每次选择“添加”时都选择“添加”,则该函数将调用自身,该函数再次调用 addstock,第一次调用永远不会完成,直到下一个调用完成(直到下一个调用永远不会完成一个等等)使用越来越多的内存等。默认的 python 安装会在你可以添加大约 1000 个股票之前崩溃并出现 RecursionError
猜你喜欢
  • 2020-07-20
  • 1970-01-01
  • 2016-06-28
  • 2015-01-24
  • 2016-07-08
  • 1970-01-01
  • 2021-01-21
  • 2018-08-13
  • 1970-01-01
相关资源
最近更新 更多