用于传递数字列表并返回每次迭代时数字平均值的函数

时间:2017-10-06 02:22:39

标签: python-3.x

我正在努力编程这个功能,我似乎很接近。问题是即使我已经定义了y = 0,当第一个循环迭代运行时,y被返回为10.它似乎是从列表中的第一个数字拉出来的,就像我更改它时一样,i和y变化

Dim strRow As String()              'String array to read all fields in a row
Dim dblAmount As Double             'Variable for total amount 
Dim blnReported As Boolean = True   'Flag to check progress report completed or not

'Create TextFieldParser class to parse the stock.csv file in the current location
Dim txtFile As FileIO.TextFieldParser



Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'Allow background operation to be cancelled
    bgWorker.WorkerSupportsCancellation = True

    'Allow background operation to report progress
    bgWorker.WorkerReportsProgress = True

    'Disable Stop Button. It is enabled only after start
    'button is pressed
    btnStop.Enabled = False

    'set status message
    lblStatus.Text = "Press Start to import data from csv."

    'Add columns to the grid
    dgvCSVData.Columns.Add("colNo", "No")
    dgvCSVData.Columns("colNo").Width = 30
    dgvCSVData.Columns.Add("colDate", "Date")
    dgvCSVData.Columns("colDate").Width = 60
    dgvCSVData.Columns.Add("colItem", "Item")
    dgvCSVData.Columns("colItem").Width = 120
    dgvCSVData.Columns.Add("colQty", "Quantity")
    dgvCSVData.Columns("colQty").Width = 50
    dgvCSVData.Columns.Add("colUnit", "Unit")
    dgvCSVData.Columns("colUnit").Width = 30
    dgvCSVData.Columns.Add("colAmt", "Amt")
    dgvCSVData.Columns("colAmt").Width = 60


End Sub
Private Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click

    'Disable Start button and Enable Stop
    btnStart.Enabled = False
    btnStop.Enabled = True

    'set status message
    lblStatus.Text = "Importing data from CSV file."

    'Start time-consuming operation in background
    Call bgWorker.RunWorkerAsync()

End Sub

Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
    'Enable Start button and disable Stop
    btnStart.Enabled = True
    btnStop.Enabled = False

    'Stop background operation
    bgWorker.CancelAsync()

    'set status message
    lblStatus.Text = "Import cancelled. Press Start again."
End Sub


Private Sub bgWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork
    Dim intCount As Int16 = 0

    txtFile = New FileIO.TextFieldParser("stock.xlsx")

    'Specify structure of the file
    txtFile.TextFieldType = FileIO.FieldType.Delimited
    txtFile.SetDelimiters(",")

    'Skip header row
    txtFile.ReadFields()

    'Start reading data from file
    While Not txtFile.EndOfData

        If bgWorker.CancellationPending Then
            e.Cancel = True
            Exit Sub

        Else

            'Wait for Progress Report to finish
            While Not blnReported
                Application.DoEvents()
            End While

            'Read all field in a row into a string array
            strRow = txtFile.ReadFields()

            'Do some calculations and assign value to data grid
            dblAmount = CDbl(strRow(3)) * CDbl(strRow(4))

            'Add some sleep to simulate a long running operation
            System.Threading.Thread.Sleep(500)

            'Progress report pending
            blnReported = False

            'increment counter
            intCount += 1

            'Report the progress
            bgWorker.ReportProgress(10, intCount)

        End If

    End While

End Sub

Private Sub bgWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgWorker.ProgressChanged
    'Copy values to data grid
    dgvCSVData.Rows.Add(strRow)
    dgvCSVData.Rows(dgvCSVData.CurrentRow.Index - 1).Cells("colAmt").Value = dblAmount

    pgbCopyProgress.Value = e.UserState
    'Progress report finished
    blnReported = True

End Sub


Private Sub bgWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted

    If e.Cancelled Or Not IsNothing(e.Error) Then
        'Clear the data grid
        dgvCSVData.Rows.Clear()
    Else
        'Progress completed
        pgbCopyProgress.Value = 100

        'Release txtfile
        txtFile.Dispose()
        btnStop.Enabled = False
        btnStart.Enabled = True

    End If
End Sub

这是为第一次迭代返回这些值。我做错了什么?其他所有东西似乎都在使用该功能。

迭代1平均值为20.0 对于Iteration 1,我是10 对于迭代1 y是10

1 个答案:

答案 0 :(得分:0)

你在循环中做的第一件事就是

  

Avg=(y+i)/z

在第一次迭代中,y为0但是我等于10,所以在做完" y + = i"之后,y等于10。 然后你做:

  

{{1}}

由于y等于10,(y + i)等于20。