Swift 变异运算符的左侧为';t可变:';gpa';是一个';让';常数

Swift 变异运算符的左侧为';t可变:';gpa';是一个';让';常数,swift,Swift,我目前正在努力解决编码课上的一个家庭作业错误。我们正在创建一个循环,循环通过gpa值数组,然后将其添加到名为totalGradePoints的变量中。问题是,当循环运行时,我遇到了一个错误: 变异运算符的左侧是不可变的:“gpa”是一个“let”常量 错误在这一行: var totalGradePoints = Double() for gpa in gpaValues { let averageGPA: Double = gpa += totalGradePoints } 这是我的全

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

变异运算符的左侧是不可变的:“gpa”是一个“let”常量

错误在这一行:

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")
/:操场-名词:人们可以玩耍的地方
导入UIKit
//你是处理转学学生成绩单的大学注册官,该成绩单包含字母和数字的混合分数。您需要将它们添加到我们的系统中,但首先您需要将字母转换为分数。
//这是一系列学生的成绩。
var转让等级:[任何]=[“C”、95.2、85、“D”、“A”、93.23、“P”、90、100]
//要准备将字母转换为数字等级,请创建一个返回双精度的函数,在该函数中创建一个开关,将a转换为95,B转换为85,C转换为75,D转换为65,P(用于传递)转换为75。其他一切都是零。
func等级转换器(字母等级:字符串)->双精度{
开关字母等级{
案例“A”:
返回95
案例“B”:
返回85
案例“C”:
返回75
案例“D”:
返回65
案例“P”:
返回75
默认值://这是其他所有值都为零的位置吗?
返回0
}
}
//创建一个名为convertedGrades的新阵列,用于存储双精度数据。
var convertedGrades:[Double]=[98.75,75.5,60.0,100.0,82.25,87.5]
//在transferGrades数组中循环,检查每个项目的类型,并将字符串(您的字母等级)发送到刚才创建的函数,并将返回的double存储在convertedGrades数组中。如果循环遇到double,可以直接将其放入新数组中,而无需转换它。如果遇到int,则需要在存储它之前将其转换为double。打印数组。(您可能会注意到,您的一些doube存储在小数点后有许多零。这不是一个错误,因此您现在可以忽略它)
在转职职系中的职系{
如果让gradeAsString=grade as?字符串{
等级转换器(字母等级:等级串)
}否则,如果让gradeAsDouble=gradeas?Double{
transferGrades.append(gradeAsDouble)
}否则,如果让gradeAsInt=grade as?Int{
双人(爬坡)
transferGrades.append(gradesInt)
}
}
印刷品(转职职系)
//现在我们有了一系列的数值分数,我们需要计算学生的平均成绩。创建一个名为GPAValues的新数组,该数组存储双精度数据。
var gpaValues:[双]=[2.5,3.0,4.0,3.12,2.97,2.27]
//与之前创建的字母转换函数和开关一样,创建一个名为calculateGPA的新函数,该函数接受一个double并返回一个double。在函数内部,创建另一个执行以下转换的开关。60岁以下的学生得零分,60岁以上的学生得1分,70岁的学生得2分,80岁的学生得3分,90岁及以上的学生得4分。
func calculateGPA(gpaValue:Double)->Double{
开关gpaValue{
案例0。。
变异运算符的左侧是不可变的:“gpa”是一个“let”常量

问题是gpa是一个常数,您不能修改它的值,“+=”运算符表示“通过totalGradePoints增加gpa的值”,它试图增加gpa的值。您可能想做的是使averageGPA等于gpa和totalGradePoints之和。为此,您可以这样做:

 let averageGPA: Double = gpa + totalGradePoints

必须使用
var
关键字定义
averageGPA
,以便在以后对值求和时使其可变

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 */

除了删除
=
平均GPA
甚至没有使用之外,还有很多问题需要解决。分配的值当然不是任何类型的平均值。这就是我需要解决的问题。非常感谢!