【问题标题】:Why does the code not put anything into the array?为什么代码没有将任何内容放入数组中?
【发布时间】:2017-04-27 20:08:35
【问题描述】:

我正在尝试创建一个可以像商店一样工作的程序,但由于某种原因,它没有在我想要的时候将任何东西放入数组中。

我使用的 csv 文件如下所示:

24937597    Basic Hatchet   20
49673494    Hardened Axe    100
73165248    Steel Axe       500
26492186    Utility Truck   2000
54963726    Small Trailer   600
17667593    Shabby Sawmill  200
76249648    SawMax 01       5000
34865729    Basic Hammer    70
46827616    50mm Nails      0.10
46827623    20mm Nails      0.05

我的代码如下所示:

import csv
import time

retry='yes'
while retry=='yes':

    receipt=[]
    total=0
    numofitems=0

    with open ('Stock File.csv','r') as stock:
        reader=csv.reader(stock, delimiter=',')

        print('Welcome to Wood R Us. Today we are selling:')
        print(' ')
        print('GTIN-8 code  Product name  Price')
        for row in reader:
            print(row[0]+'  '+row[1]+'  '+row[2])
        print(' ')

        choice='product'
        while choice=='product':
            inputvalid='yes'
            barcode=input('Enter the GTIN-8 code of the product you wish to purchase: ')
            quantity=int(input('Enter the quantity you wish to purchase: '))
            for row in reader:
                if barcode in row:
                    cost=int(row[2])
                    price=quantity*cost
                    total=total+price
                    receipt.append(barcode+row[1]+str(quantity)+row[2]+str(price))
                    numofitems=numofitems+1

            print('Do you want to buy another product or print the receipt?')
            choice=input('product/receipt ')

        if choice=='receipt':
            inputvalid='yes'
            for i in range(0, numofitems):
                print(str(receipt[i]))
                time.wait(0.5)
            print(' ')
            print('Total cost of order     '+str(total))

        else:
            inputvalid='no'

        if inputvalid=='no':
            print('Invalid input')

        if inputvalid=='yes':
            print(' ')
            print('Do you want to make another purchase?')
            retry=input('yes/no ')
        while retry!='yes':
            while retry!='no':
                print(' ')
                print('Invalid input')
                print('Do you want to make another purchase?')
                retry=input('yes/no ')
            retry='yes'
        retry='no'
if retry=='no':
    print('Goodbye! See you again soon!')

有谁知道如何解决这个问题?

【问题讨论】:

  • 既然有内置的布尔值,为什么还要使用字符串值?
  • 你为什么要反复循环reader
  • 什么数组? receipt=[] ?使用print() 检查不同代码位置的变量值。也许您在错误的地方使用receipt=[] 删除了所有值。
  • 请注意,您执行了两次for row in reader。整个文件第一次用完,所以第二次没有什么可做的。您可能希望在打印内容后立即退出 with 块并为其余代码执行另一个。您也可以在其中添加stock.seek(0)
  • 您知道您的 csv 文件只有一列吗?里面没有逗号。

标签: python arrays csv append


【解决方案1】:

在第 26 行调用 stock.seek(0)for row in reader:,然后再次读取 csv 的行。

csv.reader() 对象的行为类似于 python 的 file.read() 方法,因为在再次读取文件内容之前,需要将文件读取器重置到文件的开头。在检查用户在while choice=='product': 行的输入之前,您已经在17 行读过csv 文件一次:

for row in reader:
    ...

并且csv.reader() 对象仍然指向csv 文件内容的末尾,并且没有更多行供读者阅读,因此代码永远不会进入下一个for row in reader: 循环。

要解决此问题,请在再次读取 csv 内容之前插入 stock.seek(0) 语句,在 if barcode in row: 处,会将 csv.reader() 重置为文件的开头,您应该会看到项目填充了您的数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多