【问题标题】:How to remember something from a loop after the loop is done?循环完成后如何从循环中记住某些内容?
【发布时间】:2021-11-10 00:18:41
【问题描述】:

我正在尝试读取一个文件,然后让它打印我从该文件放入字典的特定整数(零售价格)。这部分我得到了。但是当我尝试在循环之外打印零售价格时,它找不到它们。如果我将它包含在循环中,该文件只会读取一行!救命!!

while line != '':
    line = fh.readline()
    line = line.replace(',', '')
    line = line.replace('"', '')
    line = line.replace(';', ',')
    linesplit = line.split(',')
    linesplit = line.split(',')
    dictionary = dict(zip(headerItems, linesplit))
    RetailPrice = dictionary.get('retailprice')
    print(RetailPrice)
    RetailPrice = Retail_Prices
    z = z + 1
    if z == 4:
        price = 'goodbye'
        price = input('Enter a valid Retail Price to find all products below that price, or type anything else to exit: ')
        print(Retail_Prices)

【问题讨论】:

  • 在询问与代码相关的问题时,始终需要为您使用的特定编程语言添加标签。请edit您的帖子添加该标签。谢谢。

标签: python loops dictionary while-loop get


【解决方案1】:

您可以将 while 循环外的关键字字典设置为 None 或空字典,然后在 while 循环中覆盖它。

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多