user6692600 user6692600 - 3 months ago 7
Swift Question

How do I get this adder to work?

I am using the Swift app for iOS 10 and whilst making a quick quiz app and I am trying to add a counter for how many questions You get correct but it won't change the variable Please help
I think it might be an error in the app but i don't know here is the code
I am 13 years old in year 9




var correct:Int = 0
func qa(question: String, answer: String) {
show(question)
var ans = ask("answer")
if (ans.lowercased() == answer) {
show("correct")
correct = correct + 1
} else {
show("wrong you numpty!")
}
}
func qaor(question: String, answer: String, answer2: String) {
show(question)
var ans = ask("answer")
if (ans.lowercased() == answer) || (ans.lowercased() == answer2) {
show("correct")
correct = correct + 1
} else {
show("wrong you numpty!")
}
}


show("What is your name?")

let name = ask("Name")

show("Hi " + name)

qa(question: "What is the name of the character played by Simon Jones in the Hitchikers Guide to the Galaxy?", answer: "arthur dent")

qaor(question: "What is voiced by Peter Jones in the Hitchikers Guide to the Galaxy?", answer: "the book", answer2: "the guide")

qa(question: "finish this sentence .doing the coastlines was always my Favourite, the rough crinkley edges, .... ", answer: "fjords")

var cf = "no"
if (correct == 0) {
var cf = "no"
} else if (correct == 1) {
var cf = "one"
} else if (correct == 2) {
var cf = "two"
} else if (correct == 3) {
var cf = "three"
}

show("you got " + cf + " questions correct out of three")

Answer

Not declare cf variable for every if. It creates local variable and use the global in show function. Instead, try this.

var cf = "no"
if (correct == 0) {
    cf = "no"
} else if (correct == 1) {
    cf = "one"
} else if (correct == 2) {
    cf = "two"
} else if (correct == 3) {
    cf = "three" 
}

show("you got " + cf + " questions correct out of three")
Comments