变异操作符的左侧不可变:' gpa'是一个“让...”不变

时间:2018-04-03 00:55:16

标签: swift

我目前正在努力解决编码课程中的家庭作业错误。我们正在创建一个循环,循环遍历gpa值数组,然后将其添加到名为totalGradePoints的变量中。问题是我在循环运行时遇到错误:

  

变异操作员的左侧不可变:' gpa'是一个“让...”恒定

错误在这一行:

var totalGradePoints = Double()
for gpa in gpaValues {
    let averageGPA: Double = gpa += totalGradePoints
}

这是我的完整代码:

//: Playground - noun: a place where people can play

import UIKit

// You are the university registrar processing a transfer student's transcripts that contains grades that are a mix of letters and numbers. You need to add them to our system, but first you need to convert the letters into grade points.
// Here's an array of the student's grades.

var transferGrades: [Any] = ["C", 95.2, 85, "D", "A", 93.23, "P", 90, 100]

// To prepare for converting the letters to numerical grades, create a function that returns a double, inside which you create a switch that will convert an A to a 95, B to 85, C to 75, D to 65, , P (for passing) to 75. Everything else will be a zero.

func gradeConverter(letterGrade: String) -> Double {
    switch letterGrade {
    case "A":
        return 95
    case "B":
        return 85
    case "C":
        return 75
    case "D":
        return 65
    case "P":
        return 75
    default: // Is this where everything else is zero?
        return 0
    }
}

// Create a new array called convertedGrades that stores doubles.
var convertedGrades: [Double] = [98.75, 75.5, 60.0, 100.0, 82.25, 87.5]

// Loop through the transferGrades array, inspecing each item for type and sending strings (your letter grades) to the function you just made and storing the returned double in your convertedGrades array. If your loop encounters a double, you can place it directly into the new array without converting it. It it encounters an int, you will need to convert it to a double before storing it. Print the array. (You may notice that some of your doulbes are stored with many zeros in the decimal places. It's not an error, so you can ignore that for now)

for grade in transferGrades {
    if let gradeAsString = grade as? String {
        gradeConverter(letterGrade: gradeAsString)
    } else if let gradeAsDouble = grade as? Double {
        transferGrades.append(gradeAsDouble)
    } else if let gradeAsInt = grade as? Int {
        Double(gradeAsInt)
        transferGrades.append(gradeAsInt)
    }
}
print(transferGrades)

// Now that we have an array of numerical grades, we need to calculate the student's GPA. Create a new array called GPAValues that stores doubles.

var gpaValues: [Double] = [2.5, 3.0, 4.0, 3.12, 2.97, 2.27]

// Like with the letter conversion function and switch you created before, make a new function called calculateGPA that takes a double and returns a double. Inside your function, create another switch that does the following conversion. Grades below 60 earn zero grade points, grades in the 60s earn 1, 70s earn 2, 80s earn 3, and 90s and above earn 4.

func calculateGPA(gpaValue: Double) -> Double {
    switch gpaValue {
    case 0..<59:
        return 0
    case 60...69:
        return 1
    case 70...79:
        return 2
    case 80...89:
        return 3
    case 90..<100:
        return 4
    default:
        return 0
    }
}

// Loop through your convertedGrades array and append the grade point value to the GPAValues array. Because your calculateGPA function returns a value, you can use it just like a varialbe, so rather than calculate the grade points and then put that varialbe in your append statement, append the actual function. i.e. myArray.append(myFunction(rawValueToBeConverted))

for gpa in gpaValues {
    gpaValues.append(calculateGPA(gpaValue: gpa))
}

// Finally, calculate the average GPA by looping through the GPA and using the += operator to add it to a variable called totalGradePoints. You may need to initialize the variable before using it in the loop. i.e. var initialized = Double()

var totalGradePoints = Double()
for gpa in gpaValues {
    let averageGPA: Double = gpa += totalGradePoints
}

// Count the number of elements in the array (by using the count method, not your fingers) and store that number in a variable called numberOfGrades. Pay attention to creating your variables with the right types. Swift will tell you if you're doing it wrong.

var numberOfGrades: Int = gpaValues.count

// Divide the totalGradePoints by numberOfGrades to store in a variable called transferGPA.

var transferGPA: Double = Double(totalGradePoints) / Double(numberOfGrades)

// Using code, add one numerical grade and one letter grade to the transferGrades array that we started with (i.e. append the values rather than manualy writing them into the line at the beginning of this file) and check that your transferGPA value updates. You'll need to append the new grades on the line below the existing transferGrades array so that your changes ripple through the playground.

transferGrades.append(97.56)
transferGrades.append("B")

2 个答案:

答案 0 :(得分:0)

必须使用averageGPA关键字定义

var,以便在汇总值时使其可变。

var averageGPA: Double = 0
for gpa in gpaValues {
    averageGPA += gpa
}   
averageGPA = averageGPA / Double(gpaValues.count)

回想一下,平均值是通过将得分相加并除以得分数来计算的。

使用let定义内容意味着以下内容将是常量。

let answer: Int = 42
answer = 43 /* Illegal operation. Cannot mutate a constant */

答案 1 :(得分:-1)

  

变异运算符的左侧不可变:'gpa'是'let'常量

问题是gpa是常量,你不能修改它的值。而“+ =”运算符意味着“通过totalGradePoints增加gpa的值”,它试图增加gpa的值。您可能要做的是使averageGPA等于gpa和totalGradePoints的总和。为此,你会这样做:

 let averageGPA: Double = gpa + totalGradePoints